定制IdentityUserRole主键 [英] Customised IdentityUserRole primary key

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

问题描述

我使用ASP.NET身份提供商和EF 6 code首先,我创建了一个自定义的有一个额外的列<$ C $ IdentityUserRole 表C> 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.

表目前有用户ID 角色ID

我想有列入IdentityUserRole表的主键列OrganisationId

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

我可以看到我的数据库中的UserRole表,并且它是一个的被用于对的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 });

但是,当我添加一个移民,无论是向上和向下的方法是空的。

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

更新(约code)

该IdentityUserRole类:

The IdentityUserRole class:

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

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

从的DbContext继承 IdentityDbContext&lt;使用者&GT;

用户 IdentityRebootUser

更新2

这是新的DbContext:

This is the new DbContext:

public class MyDbContext : IdentityDbContext<User, Role, string, 
                             IdentityUserLogin, UserRole, IdentityUserClaim>

这是我的新用户等级:

and this is my new User class:

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&LT; TUSER,TRole,TKEY的,TUserLogin,TUserRole,TUserClaim&GT;'。
  有没有从隐式引用转换
  MyNamespace.Model.Entities.User来
  Microsoft.AspNet.Identity.EntityFramework.IdentityUser&LT;字符串,Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin,MyNamespace.Model.Entities.UserRole,Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim&GT;

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的。我创建了具有以下签名我自己的用户对象:

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'到'Microsoft.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与我的自定义用户对象的方式,我会很乐意。

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

推荐答案

您需要自定义您的所有身份对象指定不同的密钥。有各种各样的在线教程,但我已经找到了最好的是从<一个href=\"http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity\">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天全站免登陆