-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Concurrency Issue -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. #2310
Comments
@ManikandanUlagu Map() is not part of EF Core - you're using some additional 3rd-party library, so it's difficult to know what's going on. Please submit a full, runnable code sample; I can't really help with partial code snippets and unknown 3rd-party libraries. |
@roji I have used AutoMapper for map DTO model to db model. it's not issue. But I have changed, Please find the below sample runnable code. also let me know if you need any other details from my end. Thanks Please find the below sample code. DB Injection: DB Model:
OnModelCreating:
Insert Method:
} Thanks |
@ManikandanUlagu the above is still a bunch of snippets - I cannot run it to see the error. To actually investigate I'd need a runnable project I can start. At the end of the day, you're somehow using the same DbContext instance from two threads. That probably means you're keeping a reference to a context somewhere and improperly using it, or something similar. PS please take a look at how your code samples look - it's hard to understand them. When posting code on github, use fenced code blocks (with ```) to make them readable and use syntax highlighting (see your original post above which I've edited). Good indentation also helps the reader understand what's going on. |
@roji Thank you for your response. For resolve my issue, I have changed my db context DI from AddDbContext to AddDbContextFactory and now its working as expected. Thanks |
Happy that you managed to find the fix. |
Hello team,
I am working on .Net 5 project with EF core and using PostgreSQL as Database and getting below error when I send two request at the same time... Any ideas of what could be causing this issue?
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. For more information on how to avoid threading issues with DbContext,
I have injected my db context as below :
services.AddDbContext(options =>
options.UseNpgsql(config["ConnectionStrings:DefaultConnection"]), ServiceLifetime.Transient);
and Sample DbContext model:
And the below method is for inserting the data
Error Summary:
@roji pls help on this. Thanks
The text was updated successfully, but these errors were encountered: