AddEntityFrameworkStores 只能使用派生自 .NET Core 2.0 中的 IdentityRo



我已将项目从 .NET Core 1.1 版本更改为 2.0 版本,但是当它尝试添加存储时,我从标识中收到错误:

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) { 
}
}

有人可以帮助我吗?

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

启动.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

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

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

然后将配置服务函数更改为启动.cs文件:

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

最后,创建数据库类:

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

你应该检查程序.cs

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddDefaultTokenProviders()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();

如果你放。AddRoles(( 您将收到错误,因此请使用<身份角色>

最新更新