更新时间:2023-02-15 20:32:05
得到它的一个小技巧:
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();