I thought I had this all figured out, but I'm hitting a brick wall.
Client sends in an object, my controller checks if user has right to edit, then proceeds to update the entity in the database. (along with any navigational property the object has).
My problem is, that I can't seem to get it work...
I tried
var obj = context.Recipes.SingleOrDefault(o=>o.ID == id);
UpdateModel(obj);
But I got a "model cannot be updated" exception.
So I tried a different approach, putting Recipe r into the parameter list (and let the model binder do the work) and call
context.Recipes.Attach(r);
but that leaves things on "Unchanged", so context.SaveChanges()
does nothing.
Is there a simple solution without me having to write obj.SomeProperty = r.SomeProperty
for EVERY property on my object?
Bonus question: if my entity has a navigational property (collection) I obviously add the new ones sent in (with ID == 0
), but how do I know which one to delete? Can I leverage EF's Change Tracking somewhere here?
For both questions: should there be a magic "compare this object with this object, and then save" method?