I’m using VS1010RC with the POCO self tracking T4 templates.
In my WCF update service method I am using something similar to the following:
using (var context = new MyContext())
{
context.MyObjects.ApplyChanges(myObject);
context.SaveChanges();
}
This works fine until I set ConcurrencyMode=Fixed on the entity and then I get an exception. It appears as if the context does not know about the previous values as the SQL statement is using the changed entities value in the WHERE clause.
What is the correct approach when using ConcurrencyMode=Fixed?
The previous values need to be in your object.
Let’s say you have a property
ConcurrencyToken:Now you can set
ConcurrencyMode.Fixedon that property. You also need to configure your DB to automatically update it.When you query the DB, it will have some value:
Now you can detach or serialize the object, but you need to include
ConcurrencyToken. So if you’re putting the object data on a web form, you’ll need to serializeConcurrencyTokento a string and put it in a hidden input.When you
ApplyChanges, you need to include theConcurrencyToken:Having
ConcurrencyMode.Fixedchanges theUPDATESQL. Normally it looks like:With
ConcurrencyMode.Fixedit looks like:…so if someone has updated the row between the time you read the original concurrency token and the time you saved, the
UPDATEwill affect 0 rows instead of 1. The EF throws a concurrency error in this case.Therefore, if any of this isn’t working for you, the first step is to use SQL Profiler to look at the generated
UPDATE.