-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Improve exception message: The DbContext of type cannot be pooled because it does not have a single public constructor #18573
Comments
We could improve the exception message: |
Just came across this error message after generating an EF context from an existing database using “dotnet ef dbcontext scaffold”. Scaffold creates two constructors one with a parameter of type DbContextOptions and another without any parameter. I wondered why the context must have exactly one constructor with a single parameter of type DbContextOptions. Couldn’t EF/Pooling not just ignore all constructors with a different signature? The requirement for just having one matching constructor means that one must delete the default constructor after every update/rebuild of the context using scaffold…which is somewhat annoying. |
@DaveSenn We will look into allowing the parameterless constructor. It won't be functional in a pooling scenario, but it may be okay to ignore it. |
Why is a limitation to use services when using pooling? |
@ajcvickers That would be great. Ignoring the default constructor would solve our issue. Adding support for DI constructor injection as @mhosman suggests would also be ok... but i don't know how EF would pick the correct constructor? (maybe only one constructor with a parameter of type DbContextOptions allowed?) |
Related: #12604 |
@mhosman The DbContext is resolved once from the first service provider scope, and then that instance is re-used from the pool in future requests. This means that dependencies aren't resolved from the request, and would end up being stale. If you need other dependencies injected, then the solution is to not use pooling; it's unlikely that you will see any perf improvement from pooling in a scenario like this anyway. |
I understand your argument if one uses resources injected from the request scope. But if one uses singleton resources? This limitation makes it very difficult to pass arguments to DbContext, for instance, for customizing it (such as table name or something depending on runtime configuration or command line). At least, one should allow the programmer to provide a function that builds the DbContext from DbContextOptions. |
return exception
Got Exceptions? Include both the message and the stack trace
Further technical details
EF Core version: 2.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: 2.2
Operating system: Win10
IDE: Visual Studio 2019 16.3
The text was updated successfully, but these errors were encountered: