c# - Why isn't Entity Framework updating the child object here? -
i have customer model mailingaddress property - navigation property address class:
public class customer { public int id { get; set; } public string name { get; set; } public virtual address mailingaddress { get; set; } } i have form posts viewmodel contains customer, along mailing address. here save method in controller:
public actionresult save(customerformviewmodel viewmodel) { if (!modelstate.isvalid) { return view("customerform", viewmodel); } if (viewmodel.customer.id == 0) { _context.customers.add(viewmodel.customer); } else { var customerindb = _context.customers .single(c => c.id == viewmodel.customer.id); _context.entry(customerindb).currentvalues.setvalues(viewmodel.customer); } _context.savechanges(); return redirecttoaction("index", "customers"); } when posting new customer, works fine (mostly, see note below) , customer created along corresponding address record. however, when edit existing entry, customer updated address not. verified updated address being passed in customer object. if add line this:
_context.entry(customerindb.mailingaddress).currentvalues.setvalues(viewmodel.customer.mailingaddress); then updated.
is child here still considered detached entity? assumed since property of customer fetching automatically saved parent. why work new record , not update?
one note new record creation - customer record created , has mailingaddress_id pointing address. address record created, customer_id null...an idea why ef not adding key on side of relationship? address model , view model code in case helps:
public class address { public int id { get; set; } public string street1 { get; set; } // snip bunch of address data properties public virtual customer customer { get; set; } } public class customerformviewmodel { // snip irrelevant properties public customer customer { get; set; } }
first of all, if customer , address in one-to-one relationship, no foreign key needed. actually, in one-to-one relatioonships primary key on dependant side of relationship foreign key principal side. secondly, when create new customer use context.customers.add(viewmodel.customer); , adds model of child models, when try update using _context.entry(customerindb).currentvalues.setvalues(viewmodel.customer); not add child navigation properties, so, have tell entityframework explicitly:
var customerindb = _context.customers .single(c => c.id == viewmodel.customer.id); _context.entry(customerindb) .currentvalues .setvalues(viewmodel.customer); var mailingaddressindb = _context.addresses .single(m => m.id = viewmodel.customer.mailingaddress.id); _context.entry(mailingaddressindb) .currentvalues .setvalues(viewmodel.customer.mailingaddress); it should work you. bit awkward. when have dozens of models, not want imagine it.
good news
the news that, there api solve problem roots. problem solved in few steps. install nuget using install-package ma.entityframework.graphmanager, configure models meet prerequisites (which easy) , handle whole graph using single line of code:
public actionresult save(customerformviewmodel viewmodel) { if (!modelstate.isvalid) { return view("customerform", viewmodel); } // define state of whole graph single line _context.addorupdate(viewmodel.customer); _context.savechanges(); return redirecttoaction("index", "customers"); } please, have @ codeproject article quick wallktrough. has example code, can download , examine it. owner of api , ready answer questions.
Comments
Post a Comment