I try to update a field of my object, and save it right away to the database.
using (var ctx = new DataModel(_connectionString))
{
var MyObject it = ctx.MyObjects.Where(someConstraint).ToList()[0];
try
{
//update check time
ctx.Refresh(RefreshMode.StoreWins, it); //making sure I have it
ctx.AcceptAllChanges(); // in case something else modified it - seems unnecessary
it.TimeProperty= DateTime.UtcNow; //Setting the field
ctx.DetectChanges(); //seems unnecessary
ctx.SaveChanges(SaveOptions.AcceptAllChangesAfterSave); //no SaveOptions changed the behavior
}
catch (OptimisticConcurrencyException)
{
_logger.DebugFormat(workerClassName + ": another worker just updated the LastCheckTime");
}
//Do some other work and/or sleep
}
When I run this in Azure emulator with 2 instances or more, I get a lot of OptimisticConcurrencyExceptions here.
I am trying to refresh the object, update one of its fields, then push those changes to the database. However, Optimistic Concurrency is preventing me.
note: The optimistic concurrency is set on a TimeStamp field which I never touch.
Why is that, and how can I fix it?