Issue: AddDbContext() in combination with Scaffolding from Database throws InvalidOperationException #13228
Labels
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
type-bug
Milestone
If you're new to EF Core the template created by the scaffolding tool (dotnet-cli) can be a bit confusing when you try to use it in combination with the AddDbContext() method. I'm not sure if it is an actual bug of the AddDbContext method or if it is something that can be clarified in the docs...
Steps to reproduce
Start with a new ASP.NET Core application (I chose the MVC template from Visual Studio - No Authentication).
Use the dotnet-cli to scaffold the code from the database.
dotnet ef dbcontext scaffold "Server=.\sqlexpress;Database=NORTHWND;Trusted_Connection=True;MultipleActiveResultSets=true" Microsoft.EntityFrameworkCore.SqlServer
This created the following NORTHWNDContext class. I did not include the "OnModelCreating" method because it is not relevant to my issue. The "OnConfiguring" method contains a check whether the DbContextOptions object has any extensions ( if(!optionsBuilder.IsConfigured) ).
In the Startup file, inside the ConfigureServices method I add the following line to add the NORTHWNDContext to Dependency Injection.
To make sure that I use the NORTHWNDContext somewhere I add the following code to the HomeController (I only included the code that I added).
When I now try to run the application and go to the homepage, we get the following exception:
Source of this issue
The problem exists because inside the OnConfiguring method, by default it checks for any Extensions added to the DbContextOptions object. If you delete the if-statement from your code, the code will now work. But out of curiosity I looked a bit deeper...
If you use services.AddDbContext(), the optionsAction delegate is a null reference, as expected. This will normally make sure that the DbContextOptions object is empty, however if you look at the following piece of code in the EntityFrameworkServiceCollectionExtensions Line 532, this will add a new CoreOptionsExtension from the DbContextOptionsBuilder to the Extensions Dictionary of the DbContextOptions. In other words, even though there are no DbContextOptions specified through the parameterless AddDbContext method, there is actually an extension added to its collection. This inadvertently makes the if-statement from the generated OnConfiguring method obsolete, since there will always be an extension. The only other thing you can do is use AddScoped(typeof(NORTHWNDContext)).
TL;DR
The AddDbContext method (used without parameters) adds a CoreOptionsExtension object, so if you check "optionsBuilder.IsConfigured" - as default from the scaffolding template - it will always return true because of that object.
I'm not sure if this is an issue with the generated template, with the AddDbContext method or with the documentation...
Further technical details
EF Core version: 2.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.8.0
The text was updated successfully, but these errors were encountered: