如何在ASP.NET实体框架中访问旧实体值 [英] How to access old entity value in ASP.NET Entity Framework
本文介绍了如何在ASP.NET实体框架中访问旧实体值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public ActionResult Edit(int id)
{
用户user = db .Users.Find(ID);
return View(user);
}
public ActionResult编辑(用户)
{
//(这里以旧用户名的访问权限)
db.Entry(user ).State = EntityState.Modified;
db.SaveChanges();
}
如果我做了user.name,我有新值,但我想访问保存前的db值。
谢谢!
解决方案
得到它的一个小技巧:
db.Users.Attach(user);
var current = db.Entry(user).CurrentValues.Clone();
db.Entry(user).Reload();
//你的用户(从db)的东西
db.Entry(user).CurrentValues.SetValues(current);
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
I have a small ASP.Net mvc3 application and I am on the edit part. I have the default template:
public ActionResult Edit(int id)
{
User user = db.Users.Find(id);
return View(user);
}
public ActionResult Edit(User user)
{
//(Here access to old user name for example)
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
}
If i do user.name I have the new value but I want to access the db value before the save.
Thanks !
解决方案
I got it work with a small trick:
db.Users.Attach(user);
var current = db.Entry(user).CurrentValues.Clone();
db.Entry(user).Reload();
//Do you user(from db) stuff
db.Entry(user).CurrentValues.SetValues(current);
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
这篇关于如何在ASP.NET实体框架中访问旧实体值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文