Asp.net core 将主键列添加到标识用户角色表

Asp.net core 将主键列添加到标识用户角色表,asp.net-core,asp.net-identity,identity,claims-based-identity,role-base-authorization,Asp.net Core,Asp.net Identity,Identity,Claims Based Identity,Role Base Authorization,我正在扩展IdentityUserRole,使其具有另一个主键/外键Id字段,称为工作空间Id 我收到此错误:“'ApplicationUserRole'不能在属性'WorkspaceId'上具有KeyAttribute,因为只能在根类型上声明主键” 这是我的ApplicationUserRole类: public partial class ApplicationUserRole: IdentityUserRole<string> { [Key, ForeignKey("

我正在扩展IdentityUserRole,使其具有另一个主键/外键Id字段,称为工作空间Id

我收到此错误:“'ApplicationUserRole'不能在属性'WorkspaceId'上具有KeyAttribute,因为只能在根类型上声明主键”

这是我的ApplicationUserRole类:

public partial class ApplicationUserRole: IdentityUserRole<string>
{

    [Key, ForeignKey("WorkspaceId")]
    public string WorkspaceId { get; set; }

    public virtual Workspace Workspace { get; set; }
}
public分部类ApplicationUserRole:IdentityUserRole
{
[键,外键(“工作空间ID”)]
公共字符串WorkspaceId{get;set;}
公共虚拟工作区{get;set;}
}
在DbContext中,我正在做:

 modelBuilder.Entity<ApplicationUserRole>(entity =>
            {
                entity.HasKey(k => new { k.RoleId, k.WorkspaceId, k.UserId });
                entity.HasOne(r => r.Workspace).WithMany();
            });
modelBuilder.Entity和

迁移时出现以下错误:

'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore'4[TUser,TRole,TContext,TKey]上的'Models.ApplicationUser'违反了'TUser'类型的约束。

这是我的应用程序用户

 public class ApplicationUser : IdentityUser<string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin>
公共类应用程序用户:IdentityUser
这是我的用户商店:

 public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, ApplicationDbContext, string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationUserToken, ApplicationRoleClaim>
公共类ApplicationUserStore:UserStore
以下是我的启动代码:

services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<ApplicationDbContext, string>()
            .AddDefaultTokenProviders()
            .AddUserStore<ApplicationUserStore>()
            .AddRoleStore<ApplicationRoleStore>();



services.AddScoped<UserStore<ApplicationUser, ApplicationRole, AsherDbContext, string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationUserToken, ApplicationRoleClaim>, ApplicationUserStore>();           
        services.AddScoped<RoleManager<ApplicationRole>>();
        services.AddScoped<SignInManager<ApplicationUser>>();
services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders()
.AddUserStore()
.AddRoleStore();
services.addScope();
services.addScope();
services.addScope();
以下是我的身份模型:

public class ApplicationUserRole : IdentityUserRole<string>
public class ApplicationUserClaim : IdentityUserClaim<string>
public class ApplicationUserLogin : IdentityUserLogin<string>
public class ApplicationRoleClaim : IdentityRoleClaim<string> 
public class ApplicationUserToken : IdentityUserToken<string>
public class ApplicationRole : IdentityRole<string, ApplicationUserRole, ApplicationRoleClaim>
公共类ApplicationUserRole:IdentityUserRole
公共类ApplicationUserClaim:IdentityUserClaim
公共类ApplicationUserLogin:IdentityUserLogin
公共类应用程序角色声明:IdentityRoleClaim
公共类ApplicationUserToken:IdentityUserToken
公共类应用程序角色:IdentityRole
更新2 删除以下行修复了约束冲突错误:

.AddEntityFrameworkStores<AsherDbContext, string>()
.AddEntityFrameworkStores()

ApplicationUserRole派生自模型中已包含的IdentityUserRole,因此需要在其上配置密钥,因为它是基类。默认情况下,IdentityDbContext将其密钥配置为{UserId,RoleId}

这里也回答了同样的问题: