How to update related entities in Entity Framework

前端 未结 3 1006
说谎
说谎 2020-12-16 16:47

I have an MVC project and using Entity Framework Code First and POCO objects for the database. For example:

public class ClassA
{
  public int? Id {get; set;         


        
相关标签:
3条回答
  • 2020-12-16 17:30

    I solved it by changing ClassA with a foreign key to ClassB, and setting the value of BId:

    public class ClassA
    {
      public int? Id {get; set;}
      public string Name { get; set;}
      public int BId {get;set;} // foreign key to B
      public virtual ClassB B {get; set;}
    }
    

    Why does this foreign key property does the job instead of the generated foreign ky of EF?

    0 讨论(0)
  • 2020-12-16 17:31

    You can't simply call:

    context.Entry(model).State = System.Data.EntityState.Modified;
    

    The entity has to be retrieved from the context first so that EF can begin tracking it. Then you'll want to apply any changes to that entity before calling context.SaveChanges().

    var entity = context.ClassAs.Find(model.Id);
    
    // set properties you want to modify on entity
    entity.Name = model.Name;
    entity.ClassB = context.ClassBs.Find(model.ClassB.Id);
    // other changes to entity as required...
    
    context.SaveChanges();
    

    This way EF is tracking entity and knows to apply an update against it.

    0 讨论(0)
  • 2020-12-16 17:35

    If you attach and then set the state to modified entity framework will send all properties for update. You don't have to take the otter approach here below as that causes a whole separate load to occur. If you do it that way theres no sense in passing in a model, only an id and then you call TryUpdateModel to fill in the properties from the form.

    Personally the attach as modified here is a bit cleaner as it doesn't require a complete round trip because of loading data

    http://geekswithblogs.net/michelotti/archive/2009/11/27/attaching-modified-entities-in-ef-4.aspx

    0 讨论(0)
提交回复
热议问题