I see that too few developpers really know how to use ApplyPropertyChanges ObjectContext’s method.
So I will enumerate some importants points.
ApplyPropertyChanges is used when you have an entity detached of its context and you would like to save the modifications on it.
You have two choices.
- You use optimistic mode. You keep from the DB the entity with the same EntityKey (using TryGetObjectByKey for example), to be sure that the entity is loaded on the context. Then, you call ApplyPropertyChanges with modified property.
- You do not use optimistic mode. Remember that the optimitic mode is done per property. You need to use ConcurrencyMode=fixed on EDM for each property to verify before SaveChanges. In this case, you need to keep a copy of your original entity, attach the copy to the context and then call ApplyPropertyChanges.
In this last case, my EntityCloner (even simple), can be very useful.
An other important point: ApplyPropertyChanges method doesn’t update relationships changes.
When I will have a little more time, I will try to do, in the same way that my CloneWithGraph, an ApplyPropertyChangesWithGraph method.