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:
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