Asp.net-Core-Mvc

AddEntityFrameworkStores 只能使用派生自 .NET Core 2.0 中的 IdentityRole 的角色呼叫

  • April 10, 2020

我已將一個項目從 .NET Core 1.1 更改為 2.0 版本,但是當它嘗試添加商店時,我從 Identity 收到錯誤消息:

services.AddIdentity<ApplicationUser, IdentityRole<long>>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

拋出的錯誤是:

只能使用派生自 IdentityRole 的角色呼叫 AddEntityFrameworkStores

這些是我的課:

public class ApplicationUser : IdentityUser<long>
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<long>, long>        
{
       public ApplicationDbContext(DbContextOptions options) : base(options) { 
       }
}

有人可以幫助我嗎?

很久沒有問這個問題了,但現在我是這樣處理的:

啟動.cs

services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<RoleManager<Role>>();

實體:

public class User : IdentityUser<int>
{
}

public class Role : IdentityRole<int>
{
}

對於同樣的問題,你可以看看這個:https ://github.com/aspnet/Identity/issues/1364

引用自:https://stackoverflow.com/questions/45722780