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 !
Now, when you add a new Student and call SaveChanges() method, EF will assign a newly generated id to the StudentID property. EF execute each INSERT command followed by SELECT scope_identity() statement. SCOPE_IDENTITY returns the last identity value inserted into an identity column in the same scope.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With