AddEntityFrameworkStores can only be called with a role that derives from IdentityRole in .NET Core 2.0

蓝咒 提交于 2020-06-13 17:34:48

问题


I have changed a project from the .NET Core 1.1 to 2.0 version, but I'm getting an error from the Identity, when It tries to add the stores:

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

The thrown error is:

AddEntityFrameworkStores can only be called with a role that derives from IdentityRole

These are my classes:

public class ApplicationUser : IdentityUser<long>
{
}

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

Someone could help me?


回答1:


Long time since I asked this question, but here's how I deal with nowadays:

Startup.cs

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

Entites:

public class User : IdentityUser<int>
{
}

public class Role : IdentityRole<int>
{
}



回答2:


For same issue, you can look at this:https://github.com/aspnet/Identity/issues/1364




回答3:


First, Create these two classes as follow: (Custom entities)

public class AppUser : IdentityUser<long>
{

}

public class AppRole : IdentityRole<long>
{
    public AppRole() : base()
    {

    }

    public AppRole(string roleName)
    {
        Name = roleName;
    }
}

Then change ConfigureServices function into Startup.cs file:

services.AddIdentity<AppUser, AppRole>()
    .AddEntityFrameworkStores<MyDbContext>()
    .AddDefaultTokenProviders();

Finally, Create db class:

public class MyDbContext : IdentityDbContext<AppUser,AppRole,long>
{
    public MyDbContext(DbContextOptions<MyDbContext> options)
       : base(options)
    {
    }
}


来源:https://stackoverflow.com/questions/45722780/addentityframeworkstores-can-only-be-called-with-a-role-that-derives-from-identi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!