Why Identity 2.0 adds a new column in AspNetUserRoles when I extend IdentityUser?

落爺英雄遲暮 提交于 2019-12-12 04:53:53

问题


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.

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

Why? What I am missing?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/26765815/why-identity-2-0-adds-a-new-column-in-aspnetuserroles-when-i-extend-identityuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!