I’m using an entity object called User. This is the function I use to save (in the repository):
public void saveUser(User user){
if (user.EntityState == System.Data.EntityState.Detached)
this._db.Users.Attach(user); // attach to the context
this._db.ObjectStateManager.ChangeObjectState(user, System.Data.EntityState.Modified);
this._db.SaveChanges();
}
This entity was created with the entity data model designer. This is changed by the view (I’m using the entity object as a model) and the save call is made by my controller.
When I edit the user, the changes get saved to the database, but the view sees the old properties. When I restart the program, the correct property values show up.
This is how I’m retrieving the object from the repository:
public IQueryable<User> GetUsers(String user_name)
{
IQueryable<User> userquery = from u in _db.Users
where u.user_name == user_name
select u ;
return userquery;
}
Controller:
public ActionResult ManageUser(String user_name)
{
IQueryable<User> users = this.users_db.getUsers(user_name);
User user = users.First();
return View(user);
}
[HttpPost]
public ActionResult ManageUser(User user){
this.users_db.saveUser(model.user);
ViewBag.message="Success";
return View(user);
}
I left out some of the exception and error checking code for brevity.
_db in GetUsers is probably an instance of ObjectContext? When are you instantiating it?
The behavior you are describing could be explained if you are keeping the same instance between requests. In this case it is returning the User objects that were already retrieved before you have updated the values in database. Refreshing them should help:
Though a better practice would be to create a new ObjectContext instance for each request.