c# - Update multiple tables in MVC Edit Action using repository -
i have pair of viewmodels references data number of tables. 1 displaying , 1 editing.
when return data display viewmodel can map relevant fields using valueinjecter injectfrom functionality.
what do next database update?
if send models update method in repository can see changes in model context doesn't pick them up. missing step or there better way of doing this?
if try modify 1 table @ time can context pick changes error follows:
store update, insert, or delete statement affected unexpected number of rows (0).
---edit---
i've updated code , moved mapping repository i'm still getting same error though debugger shows entities new values.
viewmodels
public partial class householdeditviewmodel //for displaying in browser { public int entityid { get; set; } public int familyid { get; set; } public string uprn { get; set; } public string address { get; set; } public housingtypedropdownviewmodel housingtypeid { get; set; } public keyworkerdropdownviewmodel keyworkerid { get; set; } public string startdate { get; set; } public bool loneparent { get; set; } public string familyphonecode { get; set; } public string familyphone { get; set; } } public partial class householdaddviewmodel //for mapping database { public int familyid { get; set; } public string uprn { get; set; } public string address { get; set; } public int entitytypeid { get; set; } public int housingtypeid { get; set; } public int keyworkerid { get; set; } public datetime startdate { get; set; } public bool loneparent { get; set; } public string familyphonecode { get; set; } public string familyphone { get; set; } }
repository (current version - i've attempted few different things without success)
public interface ihouseholdrepository : idisposable { //other methods here... void update(householdaddviewmodel model, int id); } public void update(householdaddviewmodel model, int id) { //check address exists var address = (from u in context.taddress model.uprn.contains(u.uprn) select u.uprn); var ae = new taddressentity(); ae.injectfrom(model); ae.entityid = id; ae.uprn = model.uprn; context.taddressentity.attach(ae); context.entry(ae).state = entitystate.modified; var e = new tentity(); e.injectfrom(model); e.entityid = id; e.entityname = model.address; e.taddressentity.add(ae); context.tentity.attach(e); context.entry(e).state = entitystate.modified; var = new taddress(); a.injectfrom(model); context.taddress.attach(a); context.entry(a).state = address.tostring() == string.empty ? entitystate.added : entitystate.modified; var hs = new thousingstatus(); hs.injectfrom(model); hs.entityid = id; context.thousingstatus.attach(hs); context.entry(hs).state = entitystate.modified; var k = new tkeyworker(); k.injectfrom(model); k.entityid = id; context.tkeyworker.attach(k); context.entry(k).state = entitystate.modified; var l = new tloneparent(); l.injectfrom(model); l.entityid = id; context.tloneparent.attach(l); context.entry(l).state = entitystate.modified; var h = new thousehold(); h.injectfrom(model); h.entityid = id; h.thousingstatus.add(hs); h.tkeyworker.add(k); h.tloneparent.add(l); context.entry(h).state = entitystate.modified; context.savechanges(); }
controller
[httppost] public actionresult edit(householdaddviewmodel model, int id) { model.entitytypeid = _repo.getentitytype(); if (modelstate.isvalid) { _repo.update(model, id); return redirecttoaction("index"); } return view("edit", id); }
the easiest way update entity using ef retrieve entity (using it's key) , apply updates object instance. ef automatically detect updates entity , apply them when call savechanges()
.
it seems if you're creating new entities , you're not adding them context aren't being picked up.
i change edit controller this
[httppost] public actionresult edit(householdaddviewmodel model, int id) { model.entitytypeid = _repo.getentitytype(); if (modelstate.isvalid) { var h = _repo.gethousehold(id); h.injectfrom(model); h.entityid = id; //... } }
Comments
Post a Comment