A second operation started on this context before a previous operation completed
Priya
1194
Points
33
Posts
|
The exception tells that db
Posted On:
19-Apr-2019 19:36
|
beginer
1544
Points
52
Posts
|
This error in some cases maybe caused by following scenario: if we call an async method but did not used await before calling the method. my problem solved by adding await before the method. however the answer might not be related to the mentioned question but it can be help to similar error.
Posted On:
19-Apr-2019 21:12
|
Brian
2376
Points
13
Posts
|
You can use lock statement to handle concurrency (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement).
In your case, I think, as you mentioned that you are calling from controller action so might be second request came before completion of fist one. In this case following code will resolve your problem:
Any other thread will be blocked from acquiring the lock and waits until the lock is released.
Posted On:
19-Apr-2019 21:31
It was worked for me - Smith 30-Apr-2019 08:05
|
Rashmi
1068
Points
19
Posts
|
I am not sure if you are using Dependency Injection to resolve your DbContext in your .net core application. If you are using native IoC from .NET Core (or any other IoC-Container) and you are getting this error, ensure to register your DbContext as Transient as:
AddDbContext or AddScoped adds the context as scoped, which may be cause troubles when working with multiple threads. Also async / await operations can cause this behavior, when using async lambda expressions. Adding db context as transient also has its drawbacks. You will not be able to make changes to some entity over multiple classes that are using the context because each class will get its own instance of your DbContext.
Posted On:
19-Apr-2019 21:44
|