Reload() not updating EF entity

99封情书 提交于 2019-12-04 11:54:44

If you need to update only selected properties you must do it manually:

db.YourEntitySet.Attach(model);
db.Entry(model).Property(m => m.SomeProperty).IsModifed = true;
// other properties here
db.SaveChanges();

After this your reload will work but because you want to reaload the model anyway you can do simply this:

var dbModel = db.YourEntitySet.Single(m => m.Id == model.Id);
dbModel.SomeProperty = model.SomeProperty;
// other properties here
db.SaveChanges();

And you dbModel as the "reloaded" one. You cannot use any authomatic approach like UpdateModel or applying current values because it always overrides everything including values which were not set by your client - EF doesn't know what is valid change.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!