Update only the changed values on Entity object
how can i automatically update my entity objects changed values and save them to db.
I hava an Action like that
public ActionResult Update()
{
User userToUpdate = new User();
TryUpdateModel<User>(userToUpdate,ValueProvider);
BaseRepository.Context.AttachTo("User",userT开发者_开发百科oUpdate);
BaseRepository.Context.SaveChanges();
return Json("");
}
ValuProvider : has the items that come from the client as post data.
The problem on this code is the code update all the values but i want to update only the changed values.
How can i find the changed values on my entity object.
You should check out the ObjectContext.ApplyPropertyChanges Method it is suppose to do what your asking for... msdn
Two options:
- On the View you could know the values that were changed by using Javascript and then you could pass that information to your controller.
- You could simply compare the previous values (which you already have since you populated a view) and check each value before updating the DB.
I prefer last option, since at this point you could also check for data validation.
This is really a problem for your data access code, not anything to do with your controller. Pick an ORM that handles this for you and forget about the problem.
精彩评论