-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Scaffold-DbContext template not compatible with DbContext pooling (AddDbContextPool) #12604
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
EF Triage: we are optimizing the code generated automatically for simple scenarios. You should expect to have to modify the code in certain cases. That said, looking at this issue now, it seems reasonable to relax the restriction on multiple constructors for DbContext Pooling. A warning should be enough. |
@divega thanks. It's obviously easy to modify the code to add a constructor, but a much bigger pain to remove it. BTW. The only reason I enabled connection pooling was because of the massive number of warning messages I'd get in the console just from initializing the context - this was unbearable when a new context was created for every request. I will need to check again with 2.1 to see if the warnings I got are still there . |
Came across this by chance, it's worth mentioning that this doesn't seem to be about connection pooling, but rather about DbContext pooling - the two are very different things. Connection pooling is typically enabled regardless of whether DbContexts are pooled or not. It may be worth updating the title accordingly etc. |
Not sure why this is marked as just an enhancement rather than a serious bug. Right now, there's no way to actually use Entity Framework's code generator without doing a lot of extra work to make it work properly. My steps were: generate a Database-First model, then I tried to generate a controller using the context and models that were made. That's not a small edge case, that's what most people use Entity Framework for. |
I agree with the comments above - at least there should be a command line option that should let you generate the code the way you want it... Use Visual Studio's Pre-Build events, and write a small
Basically what this script does is, it runs the powershell in a non-interactive mode with no profile (for faster loading), and replaces the constructor code in the DbContext.cs file with a method called Foo. saves the file (overwrites) DbContext.cs file. Hopefully this helps someone! |
Using aspnetcore 5.0, in switching to Similar to what @skalavala has done, this is my final scaffolding script (removes the entire constructor instead of replacing it):
|
…hen using context pooling Fixes #12604
Edit: This issue relates specifically to EF Core DbContext pooling and not more general 'connection pooling'.
After updating to EF Core 2.1 I noticed that an additional constructor (which passes through
options
) is now being included by the template. I previously had to add theDbContextOptions
constructor in a partial class to get connection pooling to work.Here is the current generated code (two constructors) :
I declare the context like this using a connection pool:
When I run it I get this error:
The presence of the additional parameterless constructor is what is causing the problem.
Workaround
Manually comment out the parameterless constructor.
Steps to reproduce
Run
Scaffold-DbContext
, and try to useservices.AddDbContextPool
Further technical details
EF Core version: 2.1.1
Database Provider: SqlServer 2.1.1
Operating system: VS 15.8 Preview 3
This did not used to occur because there were no parameters, in fact I like I said I used to have to manually add the constructor.
The text was updated successfully, but these errors were encountered: