InvalidOperationException, A second operation started on this context before a previous operation completed.

InvalidOperationException, A second operation started on this context before a previous operation completed.

Click to Watch Video

Problem: InvalidOperationException: A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations.

Solution: When calling your method prefix it with an "await" in front, e.g. await yourObject.MethodName(); Chances are that you defined your function as an async hence when calling the function you are not using "await". The error tells you exactly the way it is, when you instantiated a class you also started the DB Context and then tried to use it on the same thread, that is a no-no, use awaits so that the call can run on a different thread. Hope this helps.

Things to Check in Order to solve "A second operation started on this context" Error

- The cause of this error could be different from my scenario, that is why this error can drive a Software Developer Insane, if you try to understand it while you are kind of stressed about the error you will not make sense of it. Instead, look carefully in your Function hierarchy and find where you are calling the Database Context. It is considered a bad practice to call a database several times in the same function, why not prepare the Data you need at the Database Side and then just call the Database once in that function?

1. Check that your function is not calling a Database Context without awaiting a response. This can happen when you disregard the results of the function you are calling. For Example:

public async Task<IActionResults> MyFunction(){
//Calling the Database here
//This will cause problems when you try to make another call the Database down the line in the same Function
_ = meCallingTheDatabaseWithoutAwaitWhenThisFunctionIsAnAsyncFunction().ConfigurerAwait(false);

//Instead add an await to the function above and it will become as
//_ = await meCallingTheDatabaseWithoutAwaitWhenThisFunctionIsAnAsyncFunction().ConfigurerAwait(false);

//An Error happens Here.
var myNewData = await _myInterFace.GetData();


2. Add a ServiceLifetime.Transient to the Service.AddContext in the Startup.cs class, this will allow the Database Context to be reused per session.

     services.AddDbContext<MyDBContext>(options =>
           _configuration.GetConnectionString("DefaultConnection"), sqlServerOptionsAction: sqlOptions => {
           }), ServiceLifetime.Transient);

Edit this Article
A second operation started on this context .Net 5 Entity Framework EF Core 6 Entity Framework published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy