James
James

Reputation: 194

Modifying an entity object gets saved to database but retrieving with .net doesn't reflect changes

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.

Upvotes: 1

Views: 316

Answers (2)

Damir Arh
Damir Arh

Reputation: 17855

_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:

_db.Refresh(RefreshMode.StoreWins, user);

Though a better practice would be to create a new ObjectContext instance for each request.

Upvotes: 1

Rafay
Rafay

Reputation: 31033

[HttpPost]
public ActionResult ManageUser(User user){
    this.users_db.saveUser(model.user);
    ViewBag.message="Success"; 
    return RedirectToAction("ManageUser");
}

or you can query the updated model again

[HttpPost]
public ActionResult ManageUser(User user){
    this.users_db.saveUser(model.user);
    ViewBag.message="Success"; 
    IQueryable<User> users = this.users_db.getUsers(user_name);
    user = users.First();
    return View(user);        
}

the reason is you are passing the same old model to the view see

[HttpPost]
public ActionResult ManageUser(User user){ <-- you get the posted model here
    this.users_db.saveUser(model.user);<-- here it is saved
    ViewBag.message="Success"; <--success msg
    return View(user);<-- and here you are passing the recieved model as it is to the view
}

Upvotes: 1

Related Questions