- What is your product version? v10.5.0
- What is your product type (Angular or MVC)? MVC
- What is product framework type (.net framework or .net core)? Net5.0
We've been experiencing some growing pains here and on watching a really informative webinar yesterday (https://blog.jetbrains.com/dotnet/2021/10/08/profiling-and-fixing-common-performance-bottlenecks-webinar-recording) we've tried to start working on some of those anti-patterns we were using (like awaits inside loops).
Now that we've started, however, we're getting the error:
A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext
What are we missing here? We can wrap things in semaphoneslim to correct this (since lock can't have async code inside) but that doesn't "smell" right at all ..
Currently efcore does not support multiple parallel operations being run on the same DbContext. See: https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/#avoiding-dbcontext-threading-issues
When you use IRepository, aspnetboilerplate's manages all dbcontext creations according to tenant and it's connection strings. The existing implementation stores created dbcontext and returns it when it is requested. See: https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.EntityFramework/EntityFramework/IDbContextProvider.cs https://github.com/aspnetboilerplate/aspnetboilerplate/blob/7cba54fce06565ad1ee2738a7bb3d254c961b908/src/Abp.EntityFramework/EntityFramework/Uow/UnitOfWorkDbContextProvider.cs#L34 https://github.com/aspnetboilerplate/aspnetboilerplate/blob/7cba54fce06565ad1ee2738a7bb3d254c961b908/src/Abp.EntityFrameworkCore/EntityFrameworkCore/Uow/EfCoreUnitOfWork.cs#L144
And here is where dbcontext created: https://github.com/aspnetboilerplate/aspnetboilerplate/blob/7cba54fce06565ad1ee2738a7bb3d254c961b908/src/Abp.EntityFrameworkCore/EntityFrameworkCore/Uow/EfCoreUnitOfWork.cs#L171
That's why running multiple parallel operations on db queries is not supported right now. You may override related part of the codes and return new dbcontext for each requests of the IDbContextProvider. That would solve the problem.