asp.net mvc 4 - Why can't DbContext.Entry(object).State be used to update references? -
based on ef code first tutorial julie lerman (http://msdn.microsoft.com/en-us/data/gg715119), using similar following code snippet update model in mvc 4 application:
public actionresult edit(int id, person person) { using (var db = new mydbcontext()) { person.address = db.addresses.find(312); db.entry(person).state = entitystate.modified; db.savechanges(); } }
this saves scalar properties of person object, not reference new address. while debugging, can see dbcontext aware of updated person. after calling savechanges(), changetracker has new person correct address , state == unchanged. however, next time @ database, see old address instead of new one...
am doing wrong? behavior design?
thanks in advance!
clarification:
this question inspired behavior of of pure object databases (such db4o , others) , inner workings of dbcontext api (or ef in general).
by changing statements this:
using (var db = new mydbcontext()) { var p = db.person.find(person.id); // variable p person's proxy... db.entry(p).currentvalues.setvalues(person); // above statement seems unnecessary, required... p.address = db.addresses.find(312); db.savechanges(); }
everything saved, expected.
what puzzles me why have use foreign key associations in our model, mr. arnold suggested, able use previous (simple) syntax update both object's references scalar properties? after calling db.entry(person), person has correct references other objects, why ef ignoring them (in contrast many object databases) , updates scalars on call savechanges()?
Comments
Post a Comment