entity framework 6 - EF6 Modified state throws exception -
i've read through of comments , feedback on same question on stackoverflow , elsewhere , none of solutions worked me.
my scenario.
i have edit screen populates data viewmodal.
once user clicks on update button, use automapper map viewmodel entity, can update data.
[httppost] public jsonresult edit(core.viewmodels.buildingviewmodel model) { viewbag.title = "building | edit"; try { var entity = model.toentity<buildingviewmodel, core.entities.building>(); transactions.broker.askfor.building.update(entity); return json(new { issuccess = true, message = "building updated", identity = entity.id }); } catch (system.exception ex) { throw ex; } }
what bugs me fact that, @ point, have no entity framework entity reference, simple poco gets mapped viewmodel.
why ef tell me cannot attach / add or modify entity because of existing primary key? not update is, taking entity exist, , saving changes down?
in uow, edit looks (commented out have tried already):
public virtual spaceresult<t> edit(t entity) { spaceresult<t> result = new spaceresult<t>(); try { this._context.entry<t>(entity).state = system.data.entity.entitystate.modified; } catch (system.exception ex) { result.iserror = true; result.message = ex.message; result.innerexception = (ex.innerexception != null) ? ex.innerexception.message : string.empty; } return result; }
i don't see purpose of turning off tracking context, seems fix problem.
am missing within ef, or trying not support?
Comments
Post a Comment