A majority of the examples I see now are either using the Code First Approach or using an older version of MVC and the Entity Framework.
Assume I have a movie to update and I get to the Edit View, in the Edit method with the Post verb, what is the proper way to update a Movie? The first Edit Method below gets me to the Edit View with the populated Movie values and the second one is the one I want to use to update, I have tried some things, but nothing updates the data.
public ActionResult Edit(int id)
{
var movie = (from m in _db.Movies1
where m.Id == id
select m).First();
return View(movie);
}
[HttpPost]
public ActionResult Edit(Movie movie)
{
try
{
// TODO: Add update logic here
//What do I need to call to update the entity?
_db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Assuming that
_db
is derived fromObjectContext
you have two options:Change the state of the entity to
Modified
:This marks all properties of
movie
as modified and will send an UPDATE statement to the database which includes all column values, no matter if the values really changed or not.Reload the original entity from the database and apply the changes to it:
ApplyCurrentValues
will mark only those properties as modified which really did change compared to the original and the UPDATE statement which will be sent to the database only includes the changed column values. So, the UPDATE statement is potentially smaller than in the first example but you have to pay the price to reload the original entity from the database.Edit
How does the second code example work?
When you run a query using the context (
_db
) Entity Framework does not only retrieve the entity from the database and assign it to the left side of the query (originalMovie
) but it actually stores a second reference internally. You can think of this internal context "cache" as a dictionary of key-value pairs - the key is the entity primary key and the value is the entity itself, the same object asoriginalMovie
refers to.ApplyCurrentValues(movie)
looks up this entity in the context's internal dictionary: It takes the key property valueId
of the passed inmovie
, searches for an entity with that key in the internal dictionary and then copies property by property from the passed in ("detached")movie
to the internal ("attached") entity with the same key. EF's change tracking mechanism marks the properties asModified
which were actually different to create later the appropriate UPDATE statement.Because of this internal reference to the original entity you do not need to hold your own reference: That's the reason why
originalEntity
is not used in the code. You can in fact remove the assignment to the local variable altogether.The example would not work if you disable change tracking when you load the original entity - for example by setting
_db.Movies1.MergeOption = MergeOption.NoTracking;
. The example relies on enabled change tracking (which is the default setting when entities are loaded from the database).I cannot say which of the two examples has better performance. That might depend on details like size of the entities, number of properties which have been changed, etc.
It's worth to note though that both approaches do not work if related entities are involved (for example
movie
refers to a category entity) and if the relationship or the related entity itself could have been changed. Setting the state toModified
and usingApplyCurrentValues
both affect only scalar and complex properties ofmovie
but not navigation properties.Your second edit method should look something like this: