Reload()不更新EF实体 [英] Reload() not updating EF entity

查看:153
本文介绍了Reload()不更新EF实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢.NET MVC实践,将实例化的类模型传递给控制器​​,该控制器已经从发布的表单值中填充了一些属性。非常方便。

  [HttpPost] 
public ActionResult DoTask(MyModel model)

模型到达,其ID和其他属性从发布的表单数据设置。
但是,假设我想保存模型,然后访问只存在于数据库中的其他属性。此时我需要重新加载模型。

  db.Entry(model).State = EntityState.Modified; 
db.SaveChanges();
db.Entry(model).Reload();
//model.relatedModel是空的,所有其他模型字段都不是由帖子设置

我希望这可以根据其id从数据库完全重新加载模型。但是对我而言并不是这样。模型属性不会从数据库加载。他们仍然是空的。



编辑



我不明白为什么 db.Entry (model).Reload(); 对我来说是失败的。但进一步的阅读开始表明,我应该在执行保存然后重新加载模型后处理dbcontext。这是真的?



编辑



WHOOPS!我有错了我现在看到,当我提交更改 db.SaveChanges()我实际上覆盖了整个模型,而不仅仅是MVC控制器设置的属性。我必须有错误的模式。如何将新值(传递到我的控制器)应用到我的数据库而不覆盖未修改的属性?这是一个非常棘手的问题,我想。

结论



原来,重新加载)按预期更新。我的问题是当我使用由控制器上下文自动实例化的模型调用我的dbcontext上的SaveChanges并将其作为参数传递给我的控制器方法时,我覆盖了我的数据库行。在这种情况下,它覆盖了所分配的属性以及未分配的属性。



我现在正在选择

  model = db.Models .Single(x => x.ID == modelID); 

然后使用UpdateModel进行更新(使用前缀调用以使其正常工作)

  UpdateModel(model,myPrefix); 
db.SaveChanges();


解决方案

如果您只需要更新所选属性,它手动:

  db.YourEntitySet.Attach(model); 
db.Entry(model).Property(m => m.SomeProperty).IsModifed = true;
//其他属性
db.SaveChanges();

此后,您的重新加载将会正常工作,但是由于您想要实现模型,您可以简单地执行此操作:

  var dbModel = db.YourEntitySet.Single(m => m.Id == model.Id); 
dbModel.SomeProperty = model.SomeProperty;
//其他属性
db.SaveChanges();

而您 dbModel 作为重新加载一。您不能使用诸如 UpdateModel 之类的任何专有方法或应用当前值,因为它总是覆盖所有内容,包括您的客户端未设置的值 - EF不知道什么是有效的更改。


i like the .NET MVC practice of getting an instantiated class model passed to the controller which already has some of its properties filled in from posted form values. very handy.

[HttpPost]
public ActionResult DoTask(MyModel model)

model arrives with its id and other properties set from the posted form data. however, suppose i want to save the model and then access additional properties that exist only in the database. i need to reload the model at this point.

db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
db.Entry(model).Reload();
//model.relatedModel is empty, as are all other model fields not set by the post

i would expect this to reload the model entirely from the database based on its id. but this does not happen for me. the model properties do not get loaded from the database. they are still empty.

EDIT

i do not understand why db.Entry(model).Reload(); is failing for me. but further reading is beginning to suggest that i should dispose of the dbcontext after performing the save and then reloading the model. is this true?

EDIT

WHOOPS! i have it all wrong. i now see that when i commit the changes db.SaveChanges() i am actually overwriting the entire model, not just the properties that have been set by the MVC controller. i must have the wrong pattern. how do i apply the new values (as passed into my controller) to my database without overwriting the non-modified properties? this is turning into a very nube question, i guess.

CONCLUSION

as it turned out, Reload() DOES update as expected. my problem was that I was overwriting my db row when i called a SaveChanges on my dbcontext using a model that was auto-instantiated by the controller context and passed as a parameter to my controller method. in that case it overwrote both the assigned properties as well as the unassigned ones.

i am now doing a select

model = db.Models.Single(x => x.ID == modelID);

and then updating it using UpdateModel (calling with a prefix to make it work)

UpdateModel(model, "myPrefix");
db.SaveChanges();

解决方案

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.

这篇关于Reload()不更新EF实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆