本文介绍了为什么在扩展IdentityUser时Identity 2.0在AspNetUserRoles中添加新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从IdentityUser,CustomUser派生了一个类.如果检查数据库表,则可以看到Identity 2.0已向AspNetUserRoles表中添加了第三列,即CustomUser_Id.

I have derived a class from IdentityUser, CustomUser.If I check the database tables, I can see that Identity 2.0 has added a third column to AspNetUserRoles table, called CustomUser_Id.

这似乎是我的CustomUser表的外键,但是AspNetUserRoles已经具有UserId列,因此似乎多余.

It seems a foreign key to my CustomUser table, but AspNetUserRoles already has UserId column, so it seems redundant.

为什么?我想念的是什么?

Why? What I am missing?

推荐答案

我假设您也有ApplicationUser闲逛. IdentityUser是抽象类,所以dbo.AspNetUserRoles中的列附加到用作通用IdentityDbContext<TUser>TUser类型的任何具体类上.默认情况下为ApplicationUser.如果您创建了IdentityUser的另一个子类,那么您要做的就是创建一个具有相似属性的类,但是该类与所有Identity东西完全断开,并且可能具有自己的表dbo.CustomUsers,然后需要一个单独的表.要在dbo.AspNetUserRoles上添加外键.

I'm assuming you've also got ApplicationUser hanging around. IdentityUser is an abstract class, so the column in dbo.AspNetUserRoles is attached to whatever concrete class is used as the TUser type for the generic IdentityDbContext<TUser>. By default, that's ApplicationUser. If you created another subclass of IdentityUser, all you've done is create a class with similar properties, but one which is totally disconnected from all the Identity stuff and has its own table, dbo.CustomUsers probably, which then requires a separate foreign key to be added on dbo.AspNetUserRoles for it.

长短不一,您只能对用户苹果一口气.如果您希望拥有不同类型的用户,则他们需要继承自您的主要用户实现(默认为ApplicationUser),而不是IdentityUser.

Long and short, you only get one bite at the user apple. If you want to have different types of users, they need to inherit from your main user implementation (ApplicationUser by default), not IdentityUser.

这篇关于为什么在扩展IdentityUser时Identity 2.0在AspNetUserRoles中添加新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 12:36