Switching from a mono webserver to a webfarm, I need to re-implement a business critical feature to make it compatible with the webfarm. The code used to be something like:
private static readonly Object lockToken = new Object();
public void ChangeState(Foo bar)
{
lock (lockToken)
{
// Change state logic
/*
*Some code
*/
Context.Entities.SaveChanges();
}
}
I know I can't keep the lock and must switch to application-managed concurrency tokens. Before doing that, and to make sure the implementation is correct I'd like to break my own code and make it throw reals DbUpdateConcurrencyException (ie: I want it to be thrown by the framework, not from my code). I tried using this code:
Parallel.For(0, 100, y =>
{
Parallel.ForEach(objects, x =>
{
try
{
service.ChangeState(new Foo(x));
}
catch (DbUpdateConcurrencyException duce)
{
count++;
LogManager.Log.Error("Concurrency Error", duce);
}
});
});
But I can't make it to happen.
The project is in .NET 4.8 / EF 6 / SQL Server 2016