I'm migrating from netcoreapp 2.1 to 3.1, and I've found a breaking change for EF core 3.1 that I can't resolve. The following worked in 2.1 so simply saying db context isn't thread safe by design and pointing to other questions that don't deal with this nuance does not address the issue at hand.
Previously in 2.1, this worked:
taskList.Add(MethodOne(myRequestObject));
taskList.Add(MethodTwo(myRequestObject));
await Task.WhenAll(taskList);
where both methods only read(never changed) from the db context and look something like this:
private async Task MethodOne(RequestObject myRequestObject)
{
var entity = await DbContext
.MyDbSet
.OrderByDescending(x => x.SomeProperty)
.FirstOrDefaultAsync(x => x.Id == myRequestObject.Id);
if (entity != null)
myRequestObject.SomeRequestProperty = entity.AnotherProperty;
}
}
In 3.1, even when I'm just reading and not changing the entities, the DB context ConcurrencyDetector thinks it has EnterCriticalSection
which causes an exception when the second method tries to await on the DbContext:
InvalidOperationException: A second operation started on this context before a previous operation completed
For baseline sanity, I also tried the following which does work(but is not an ideal solution for the real code of my app):
await MethodOne(myRequestObject);
await MethodTwo(myRequestObject);
So here is my questions:
- Is there a way to continue to tell EF core 3.1 to allow concurrency when I know it is safe. this worked in 2.1 so clearly this cannot be dismissed by simply saying db context has never allowed this by design and closing the question as a duplicate. My app has been running happily in production for a long time. Only after migrating to 3.1 has this become an issue. What has changed? Is this change truly impossible to work around or are there exceptions to the claim that the context doesn't allow this? If this worked before when this 'wasn't allowed by design', is it possible that it is similarly not true now as well?