自定义 IdentityUserRole 主键 [英] Customised IdentityUserRole primary key

查看:20
本文介绍了自定义 IdentityUserRole 主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ASP.NET Identity Provider 和 EF 6 Code First,我创建了一个自定义的 IdentityUserRole 表,其中有一个额外的列 OrganisationId.自定义表名为 UserRole.

I'm using ASP.NET Identity Provider and EF 6 Code First and I have created a custom IdentityUserRole table which has an extra column OrganisationId. The custom table is named UserRole.

该表当前具有UserIdRoleId的默认主键.

The table currently has the default primary key of UserId and RoleId.

我希望在 IdentityUserRole 表的主键中包含 OrganisationId 列.

I would like to have the OrganisationId column included in the Primary Key of the IdentityUserRole table.

我可以在我的数据库中看到 UserRole 表,它是用于用户角色的表(没有 aspnet_userrole 表,当我为用户分配角色时,该表有数据)

I can see the UserRole table in my database, and it's the one that's being used for the userroles (there's no aspnet_userrole table and the table has data when I assign a role to a user)

我试过了:

modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole").HasKey(x => new { x.UserId, x.RoleId }); 

但它没有向我显示 OrganisationId 属性.

But it doesn't show me the OrganisationId property.

所以我尝试了:

modelBuilder.Entity<UserRole>().HasKey(x => new { x.OrganisationId, x.RoleId, x.UserId });

但是当我添加迁移时,Up 和 Down 方法都是空的.

But when I add a migration, both the Up and Down methods are empty.

更新(一些代码)

IdentityUserRole 类:

The IdentityUserRole class:

public class UserRole : IdentityUserRole, IOrganisationEntity
{
    [Key]
    public int OrganisationId { get; set; }

    [ForeignKey("OrganisationId")]
    public Organisation Organisation { get; set; }
}

DbContext 继承自 IdentityDbContext

DbContext inherits from IdentityDbContext<User>

User继承自IdentityRebootUser

更新 2

这是新的 DbContext:

This is the new 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>

但是,这给了我以下错误:

However, this gives me the following error:

类型MyNamespace.Model.Entities.User"不能用作类型泛型类型或方法中的参数TUser"'Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext'.没有隐式引用转换来自'MyNamespace.Model.Entities.User' 到'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'

The type 'MyNamespace.Model.Entities.User' cannot be used as type parameter 'TUser' in the generic type or method 'Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim>'. There is no implicit reference conversion from 'MyNamespace.Model.Entities.User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser<string, Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin, MyNamespace.Model.Entities.UserRole, Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim>'

更新 3

我摆脱了 IdentityReboot.我创建了自己的 User 对象,它具有以下签名:

I got rid of IdentityReboot. I created my own User object which has the following signature:

public class User : IdentityUser<string, IdentityUserLogin, 
                        UserRole, IdentityUserClaim>

我现在可以修改 UserRole 表以添加额外的 PK,这正是我想要的.

I can now modify the UserRole table to add an extra PK, which is what I wanted.

我现在遇到的问题是,我不能使用默认的 UserStore,因为当我告诉它使用我的自定义用户"表时,我收到以下错误:

The problem I have now is, that I cannot use the default UserStore, because when I tell it to use my custom 'User' table, I get the following error:

没有从MyDB.Model.Entities.User"到M​​icrosoft.AspNet.Identity.EntityFramework.IdentityUser"的隐式引用转换.

There is no implicit reference conversion from 'MyDB.Model.Entities.User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.

所以我实现了我自己的 IUserStore 版本,它会产生异步错误.

So I implemented my own version of IUserStore, which is generating the async errors.

如果有一种方法可以将 UserStore 与我的自定义 User 对象一起使用,我会非常高兴.

If only there was a way to use UserStore with my custom User object, I would be more than happy.

推荐答案

您需要自定义所有标识对象以指定不同的键.网上有各种教程,但我找到的最好的是 www.asp.net.

You need to customise all of your identity objects to specify different keys. There are various tutorials online but the best I've found is from www.asp.net.

本质上,您需要为每个对象制作自己的版本,但要从更复杂的版本继承.例如,代替:

Essentially you need to make your own version of each object but inherit from the more complex versions. For example, instead of:

public class ApplicationUser : IdentityUser
{
    ...
}

你会这样做:

public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, 
CustomUserClaim> 
{ 
    ...
}

并重复所有对象,包括 UserStore、UserManager、RoleManager 等

And repeat for all object including UserStore, UserManager, RoleManager etc.

这篇关于自定义 IdentityUserRole 主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆