I know that using ObjectStateManager just results in 1 trip to the database, but a larger update statement and ApplyCurrentValues results in 2 trips to the database, one to retreive the entity and one to update it, so what are the pros and cons of each also, how does issuing the following statement know that I want to update that current record, I know it has something to do with loading it into context, but not sure how it works.
[HttpPost]
public ActionResult Edit(Movie movie)
{
(from m in _db.Movies1
where m.Id == movie.id
select m).First()
//How does calling the above query know to update the movie,
can't I do it with out it?
_db.Movies1.ApplyCurrentValues(movie);
_db.SaveChanges();
}
The code you posted ‘knows’ you want to update the record because it assumes if the state is modified (which applycurrentvalues will mark properties as modified) then you must want to update that field.
So anything that is modified is set, and then sent to the database. However the code that has .First() in it does nothing helpful here.
ApplyCurrentValues does not load anything, so your first line of code loads it into the context. Then you ApplyCurrentValues merges the values into it. Anything that is different is marked as modified and copied over then those fields only are sent to the db for update.
You can profile the activity, check out
http://msdn.microsoft.com/en-us/magazine/gg490349.aspx
also there is a nice demp profiler available at http://efprof.com/
Another option is to simply attach as modified and save changes. I think that will only yield one query (the update to the database), I’ll have to double check that.
That code would look like:
That will save every property, but I think it may be one less trip (update, reload, as opposed to load, update, reload)