How can I update data related to Entity Framework Core 2.1?

1

I am trying to update some records of my entity User , this one has a pair of foreign keys and I also want those records to be updated but not a field of my main entity that is User , so that:

My model is:

public class User
{
    public int Id { get; set; }
    public int ProfileId { get; set; }
    public int UserTypeId { get; set; }
    public string Username { get; set; }
    public string Skype { get; set; }
    public int SalutationId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Password { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime? DateModified { get; set; }
    public bool Active { get; set; }

    public List<Email> Emails { get; set; }
    public List<Phone> Phones { get; set; }        
    public List<UserCenter> UserCenter { get; set; }        
}

And I try to update it like this:

 private void UpdateUser(User user)
 {
      user.DateModified = DateTime.Now;

      using (var context = new LCDPContext())
      {
          var input = context.Attach(user);

          input.Property(p => p.Username).IsModified = true;
          input.Property(p => p.SalutationId).IsModified = true;
          input.Property(p => p.Active).IsModified = true;
          input.Property(p => p.FirstName).IsModified = true;
          input.Property(p => p.LastName).IsModified = true;
          input.Property(p => p.Skype).IsModified = true;
          input.Property(p => p.Password).IsModified = true;
          input.Property(p => p.UserTypeId).IsModified = true;
          input.Property(p => p.ProfileId).IsModified = true;
          input.Property(p => p.DateModified).IsModified = true;              

          context.SaveChanges();
      }
 }

I'm not indicating the property DateCreated But I do not know how to update the related data.

    
asked by Arthur 20.11.2018 в 18:27
source

0 answers