【问题标题】:AddEntityFrameworkStores can only be called with a role that derives from IdentityRole in .NET Core 2.0AddEntityFrameworkStores 只能使用派生自 .NET Core 2.0 中的 IdentityRole 的角色调用
【发布时间】:2017-08-16 20:50:06
【问题描述】:

我已将一个项目从 .NET Core 1.1 更改为 2.0 版本,但是当它尝试添加商店时,我从 Identity 收到错误:

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

抛出的错误是:

AddEntityFrameworkStores 只能使用派生的角色调用 来自身份角色

这些是我的课程:

public class ApplicationUser : IdentityUser<long>
{
}

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

有人可以帮助我吗?

【问题讨论】:

  • 您找到解决方案了吗?
  • 同样的问题,你找到解决办法了吗?

标签: asp.net-core-mvc asp.net-identity .net-core asp.net-core-2.0


【解决方案1】:

我很久没有问这个问题了,但现在我是这样处理的:

Startup.cs

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

实体:

public class User : IdentityUser<int>
{
}

public class Role : IdentityRole<int>
{
}

【讨论】:

    【解决方案2】:

    同样的问题,你可以看看这个:https://github.com/aspnet/Identity/issues/1364

    【讨论】:

    • 非常混乱的链接...转向如此多的切线...除非我将下一小时的生产开发投入到涉水其中,否则我无法分辨哪个是相似的
    【解决方案3】:

    首先,按如下方式创建这两个类:(自定义实体)

    public class AppUser : IdentityUser<long>
    {
    
    }
    
    public class AppRole : IdentityRole<long>
    {
        public AppRole() : base()
        {
    
        }
    
        public AppRole(string roleName)
        {
            Name = roleName;
        }
    }
    

    然后将ConfigureServices函数改成Startup.cs文件:

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

    最后,创建 db 类:

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

    【讨论】:

      猜你喜欢
      • 2018-07-03
      • 2021-12-18
      • 2018-02-05
      • 2018-09-13
      • 2018-09-05
      • 2018-04-03
      • 1970-01-01
      • 2017-09-07
      • 2018-08-14
      相关资源
      最近更新 更多