An attempt was made to use the context while it is being configured. A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point.
See original GitHub issueI am trying to use DI for my dbContext with MVC6/EF7 RC1.
I am instantiating my ContextTest (inherited from dbcontext) using [FromServices] attribute in the constructor of my TestService class
Sample code:
[FromServices]
public ContextTest _testContext { get; set; }
public TestService(ContextTest testContext)
{
_testContext = testContext;
}
public async GetData()
{
await _testContext.GetFunA();
await _testContext.GetFunB();
await _testContext.GetFunC();
}
Startup.cs
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ContextTest >(options => options.UseSqlServer(connection))
Exception:
Sporadically i am running into below exception
“An attempt was made to use the context while it is being configured. A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point.”
My 2 cents: Something to with Async calls (DI injects only 1 instance and i am trying to fire 3 queries in parallel.
Any suggestions?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:19 (8 by maintainers)
Top Results From Across the Web
An attempt was made to use the context while it is being ...
A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point. This can happen if a second...
Read more >DbContext Lifetime, Configuration, and Initialization
This means OnConfiguring can be used to perform additional configuration even when AddDbContext is being used. Configuring the database provider.
Read more >Issues in Optimizely when creating large number of users ...
1. System.InvalidOperationException: An attempt was made to use the context while it is being configured. A DbContext instance cannot be used ...
Read more >Asp.net-core – A DbContext instance cannot be used ...
... An attempt was made to use the context while it is being configured. A DbContext instance cannot be used inside OnConfiguring since...
Read more >Best Practices in Using the DbContext in EF Core
This article talks about Db Context concepts and the best practices you can adhere to for improving the performance and scalability of your ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@valcs007 you are using DbContext in TestService, which is possibly singleton and you end up using the one and only DbContext in your whole app. I’ve ran into same problem due to inaccurate design of my services: controller used some service injected through constructor to actually perform business logic, while this service was a singleton with DbContext injected through constructor again. So I ended up trying to operate on the same DbContext in every request. When there were few requests, it worked fine, but under some pressure bunch of exceptions begin to occur like
Be careful and pay attention to register all of your services with correct DI lifetime management. Do not inject Scoped and Transient services into singletons. P.S. I hope my comment will someday help someone 😃
@durilka DbContext is not thread safe. It is never safe to have the same instance being accessed in parallel.