In one of the videos of RIA services of Pluralsight I was referring, they had an update code like this:
public void UpdateProspect(Prospect currentProspect)
{
currentProspect.LastUpdate = DateTime.Now;
ObjectContext.Prospects.AttachAsModified(currentProspect, ChangeSet.GetOriginal(currentProspect));
}
My first question is how will this update cause problems? To show the problems he starts two Silverlight clients, then from first client he updates the item and it updates nicely. Then he goes to the second client and makes an update and it throws an error.
Why is RIA throwing errors? What has it got to do with the first update? This is really buggy I think and we need to again write some special code to resolve the EntityConflict and then again submit the batch to the server. Is this appropriate?
He also set the ConcurrencyMode to Fixed. My second question is when would you set the ConcurrencyMode to Fixed? By default, the mode is set to None.
This is because of a race condition caused by a Stale Read. Consider the following:
Prospect
- lets call it version 1LastUpdate
property, the object is now version 2Prospect
LastUpdate
property. To Client B, the version is now version 2 but Client B's version 2 is different from Client A'sProspect
- this would overwrite Client A's changes!The problem here is that B can't know that A has made any changes. The Exception is thrown to prevent the race condition from accidentally deleting data.
The solution is to catch this exception and report that the Prospect
was changed while you were editing it, and then reload it.
You can find more information about using ConcurrencyMode=fixed
here.