views:

111

answers:

3

Hi, 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",userToUpdate);
      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.

A: 

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.

Freddy
Thanks for your answer, i want to ask one more thinkHow can i find the changed values on my entity object.
Yucel
A: 

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.

Wyatt Barnett
A: 

You should check out the ObjectContext.ApplyPropertyChanges Method it is suppose to do what your asking for... msdn

moi_meme