ASP.NET API - Unable to create a DbContext

Mahesh Kumar 60 Reputation points
2024-12-05T18:20:36.1133333+00:00

I am learning ASP.NET

I tried the following code to enable IdentityCore on my API with AddRole

builder.Services.AddIdentityCore<User>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders()
.AddApiEndpoints();

...

app.MapIdentityApi<User>();

...

public class AppDbContext : IdentityDbContext<User> { ...

public class User : IdentityUser
{

    
}

These are the codes that i have used.
Before adding the AddIdentityCore, Everything was working. I was able to connect with the Db

builder.Services.AddDbContext<AppDbContext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("PgDatabase")));

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.DataProtectorTokenProvider`1[backend.Models.User] Lifetime: Transient ImplementationType: Microsoft.AspNetCore.Identity.DataProtectorTokenProvider`1[backend.Models.User]': Unable to resolve service for type 'Microsoft.AspNetCore.DataProtection.IDataProtectionProvider' while attempting to activate 'Microsoft.AspNetCore.Identity.DataProtectorTokenProvider`1[backend.Models.User]'.)


Unable to create a 'DbContext' of type 'backend.Database.AppDbContext'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions' while attempting to activate 'backend.Database.AppDbContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728


Microsoft Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
728 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,704 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,736 Reputation points Microsoft Vendor
    2024-12-06T02:30:29.0933333+00:00

    Hi @Mahesh Kumar

    builder.Services.AddIdentityCore<User>() .AddRoles<IdentityRole>() .AddEntityFrameworkStores<AppDbContext>() .AddDefaultTokenProviders() .AddApiEndpoints();

    The issue relates the above code, incorrect Identity configuration. When I try to use the above code and enable migration, I could reproduce the problem:

    User's image

    To solve this issue, you can try to modify the code as below:

    builder.Services.AddIdentity<User, IdentityRole>()
                    .AddEntityFrameworkStores<AppDbContext>()
                    .AddDefaultTokenProviders()
                    .AddApiEndpoints();
    

    Besides, if you want to use the AddRoles method to configurate Identity Role, you can use it with AddIdentityApiEndpoints<IdentityUser> or AddDefaultIdentity<IdentityUser>(), code like this:

    builder.Services.AddIdentityApiEndpoints<User>()
        .AddRoles<IdentityRole>()
        .AddEntityFrameworkStores<AppDbContext>();
    

    or

    builder.Services.AddDefaultIdentity<User>()
                    .AddRoles<IdentityRole>()
                    .AddEntityFrameworkStores<AppDbContext>()
                    .AddDefaultTokenProviders()
                    .AddApiEndpoints();
    

    Note: The preceding code requires the Microsoft.AspNetCore.Identity.UI package and a using directive for Microsoft.AspNetCore.Identity. Generally, ASP.NET Core Identity UI is the default Razor Pages built-in UI for the ASP.NET Core Identity framework, and we will use the Microsoft.AspNetCore.Identity.UI package in the Razor Page or MVC application.

    The above codes work well on my side. After migration, the database generated successfully. You can check them.

    More detail information, see:

    Identity model customization in ASP.NET Core

    Role-based authorization in ASP.NET Core


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.