When adding blazor scaffolding AddDBContext overwrites AddDBContextFactory and so cause app not to build #59423
Closed
1 task done
Labels
area-blazor
Includes: Blazor, Razor Components
Is there an existing issue for this?
Describe the bug
I created an interactive Server Blazor Web app with Identity.
The program.cs correctly had:
builder.Services.AddDbContextFactory(options =>
options.UseSqlServer(connectionString));
I then added the movie.cs example from Dan Roth using Blazor scaffolding. The program.cs got overwritten to
builder.Services.AddDbContext(options =>
options.UseSqlServer(connectionString));
This caused the following error:
An unhandled exception occurred while processing the request.
InvalidOperationException: Cannot provide a value for property 'DbFactory' on type 'BlazorAppMovieTest.Components.Pages.MoviePages.Index'. There is no registered service of type 'Microsoft.EntityFrameworkCore.IDbContextFactory`1[BlazorAppMovieTest.Data.ApplicationDbContext]'.
To fix this issue, I had to manually change AddDbContext back to AddDbContextFactory, and then it worked as expected.
Expected Behavior
Using Blazor CRUD scaffolding does not break the application
Steps To Reproduce
I created an interactive Server Blazor Web app with Identity.
The program.cs correctly had:
builder.Services.AddDbContextFactory(options =>
options.UseSqlServer(connectionString));
I then added the movie.cs example from Dan Roth using Blazor scaffolding. The program.cs got overwritten to
builder.Services.AddDbContext(options =>
options.UseSqlServer(connectionString));
This caused the following error:
An unhandled exception occurred while processing the request.
InvalidOperationException: Cannot provide a value for property 'DbFactory' on type 'BlazorAppMovieTest.Components.Pages.MoviePages.Index'. There is no registered service of type 'Microsoft.EntityFrameworkCore.IDbContextFactory`1[BlazorAppMovieTest.Data.ApplicationDbContext]'.
To fix this issue, I had to manually change AddDbContext back to AddDbContextFactory, and then it worked as expected.
Exceptions (if any)
An unhandled exception occurred while processing the request.
InvalidOperationException: Cannot provide a value for property 'DbFactory' on type 'BlazorAppMovieTest.Components.Pages.MoviePages.Index'. There is no registered service of type 'Microsoft.EntityFrameworkCore.IDbContextFactory`1[BlazorAppMovieTest.Data.ApplicationDbContext]'.
.NET Version
dotnet 9.0
Anything else?
No response
The text was updated successfully, but these errors were encountered: