我正在使用 ASP.NET Identity Provider 和 EF 6 Code First,并且我创建了一个自定义IdentityUserRole
有额外列的表OrganisationId
。自定义表名为 UserRole。
该表当前的默认主键为UserId
and RoleId
.
我希望将 OrganizationId 列包含在 IdentityUserRole 表的主键中。
我可以在数据库中看到 UserRole 表,它是用于 userroles 的表(没有 aspnet_userrole 表,当我将角色分配给用户时,该表有数据)
我试过这个:
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole").HasKey(x => new { x.UserId, x.RoleId });
但它没有向我显示 OrganizationId 属性。
所以我尝试:
modelBuilder.Entity<UserRole>().HasKey(x => new { x.OrganisationId, x.RoleId, x.UserId });
但是当我添加迁移时,Up 和 Down 方法都是空的。
更新(部分代码)
IdentityUserRole 类:
public class UserRole : IdentityUserRole, IOrganisationEntity
{
[Key]
public int OrganisationId { get; set; }
[ForeignKey("OrganisationId")]
public Organisation Organisation { get; set; }
}
DbContext 继承自IdentityDbContext<User>
And User
继承自IdentityRebootUser
Update 2
这是新的 DbContext:
public class MyDbContext : IdentityDbContext<User, Role, string,
IdentityUserLogin, UserRole, IdentityUserClaim>
这是我的新用户类:
public class User : IdentityRebootUser<string, IdentityUserLogin,
Role, IdentityUserClaim>
和角色类:
public class Role : IdentityRole<string, UserRole>
但是,这给了我以下错误:
类型“MyNamespace.Model.Entities.User”不能用作类型
泛型类型或方法中的参数“TUser”
'Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext'。
没有隐式引用转换
'MyNamespace.Model.Entities.User' 到
'Microsoft.AspNet.Identity.EntityFramework.IdentityUser '
Update 3
我摆脱了 IdentityReboot。我创建了自己的 User 对象,它具有以下签名:
public class User : IdentityUser<string, IdentityUserLogin,
UserRole, IdentityUserClaim>
我现在可以修改 UserRole 表以添加额外的 PK,这正是我想要的。
我现在遇到的问题是,我无法使用默认的 UserStore,因为当我告诉它使用我的自定义“用户”表时,我收到以下错误:
没有从“MyDB.Model.Entities.User”到“Microsoft.AspNet.Identity.EntityFramework.IdentityUser”的隐式引用转换。
所以我实现了我自己的 IUserStore 版本,它会生成异步错误。
如果有一种方法可以将 UserStore 与我的自定义 User 对象一起使用,我会非常高兴。