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 issue

I 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:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

32reactions
dancecommented, Apr 11, 2016

@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

Invalid operation. The connection is closed.
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.
There is already an open DataReader associated with this Command which must be closed first.
The connection was not closed. The connection's current state is connecting.
The context cannot be viewed while the model is being created

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 😃

6reactions
ajcvickerscommented, Jun 9, 2016

@durilka DbContext is not thread safe. It is never safe to have the same instance being accessed in parallel.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found