【问题标题】:Add primary key column to Identity User Roles Table将主键列添加到身份用户角色表
【发布时间】:2018-09-25 18:24:27
【问题描述】:

我正在扩展 IdentityUserRole 以拥有另一个名为 WorkspaceId 的主键/外键 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; }
}

在 DbContext 中我正在做:

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

向我的 UserRoles 表添加主键的最佳方法是什么,或者,创建另一个表是否更好、更安全?对我来说,拥有另一个包含来自 IdentityUserRole 的 PK/FK 和 WorkspaceId PK/FK 的表似乎有点矫枉过正。这意味着我需要维护这两个表。

另外,我使用的是 .net Core 1.1

更新 1

在审查和实施调查结果后:herehere

迁移时出现以下错误:

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

这是我的应用程序用户

 public class ApplicationUser : IdentityUser<string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin>

这是我的用户存储:

 public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, ApplicationDbContext, string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationUserToken, ApplicationRoleClaim>

这是我在 Startup 中的代码:

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>>();

这是我的身份模型:

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>

更新 2 删除以下行修复了约束冲突错误:

.AddEntityFrameworkStores<AsherDbContext, string>()

【问题讨论】:

    标签: asp.net-core asp.net-identity identity claims-based-identity role-base-authorization


    【解决方案1】:

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

    这里已经回答了同样的问题:

    Customised IdentityUserRole primary key

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-24
      • 2017-05-16
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      相关资源
      最近更新 更多