c# - Updating related Data with Entity Framework 6 in MVC 5 -


using entityframework 6, update customer in following scenario:

public class customer {     public int id {get; set;}     // 100 more scalar properties     public virtual ilist<consultant> consultants {get;set;} }  public class consultant {     public int id {get; set;}     public virtual ilist<customer> customers{get;set;} } 

this viewmodel edit view:

public class customerviewmodel {     public string[] selectedconsultants { get; set; }     public ienumerable<consultants> allconsultants{ get; set; }     public customer customer{ get; set; } } 

this edit-actionmethod:

    [httppost]     public actionresult edit(customerviewmodel vm)     {         if (modelstate.isvalid)         {             // update scalar properties on customer             var updatedcustomer = vm.customer;             _db.customers.attach(updatedcustomer );             _db.entry(updatedcustomer ).state = entitystate.modified;             _db.savechanges();              // update navigational property [consultants] on customer             var customer = _db.customers                         .include(i => i.customers)                         .single(i => i.id == vm.customer.id);              customer.consultants.clear();             _db.consultants.where(x => vm.selectedconsultants                        .contains(x.id)).tolist()                        .foreach(x => customer.consultants.add(x));              _db.entry(customer).state = entitystate.modified;             _db.savechanges();             return redirecttoaction("index");         }         return view(vm);     } 

this works , both scalar properties , consultants updateable edit view. however, doing 2 _db.savechanges(); in controller. there less complex way update customer? because customer has many properties, i'd preferably not manual matching of parameters on customer , vm.customer.

i have found following resources:

  1. asp.net official seems overly complicated (see section adding course assignments instructor edit page) plus require me explicitly write parameters of customer)
  2. this popular thread on so. method 3 looks need not navigational property updated.

i don't think it's necessary call savechanges twice.

have tried this:

      var customer = _db.customers                         .where(c => c.id== vm.customer.id)                         .include(c => c.consultants)                         .singleordefault();        customer.consultants = _db.consultants                                 .where(x => vm.selectedconsultants.contains(x.id)).tolist();        _db.savechanges(); 

edit:

ok, not sure if work, can try using automapper:

            var customer = mapper.map<customer>(vm.customer);             _db.entry(customer).state = entitystate.modified;              customer.consultants = _db.consultants.where(x => vm.selectedconsultants.contains(x.id)).tolist();              _db.savechanges(); 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -