Skip to content

When adding blazor scaffolding AddDBContext overwrites AddDBContextFactory and so cause app not to build #59423

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

Closed
1 task done
coderdnewbie opened this issue Dec 10, 2024 · 2 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@coderdnewbie
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

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

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Dec 10, 2024
@coderdnewbie
Copy link
Author

I will try to recreate this again, it is doing AddDbContext everywhere now

@juliomartinezcarrasco
Copy link

I had same issue and same solution already explained works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

2 participants