如何使用身份和.net core 2创建两种类型的用户 [英] How to create two type of users with identity and .net core 2

查看:61
本文介绍了如何使用身份和.net core 2创建两种类型的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用.net core 2和Web服务开发Web应用程序.

I start to develop an web application with .net core 2 and web service..

当用户在站点上注册时,他可以选择自己是公司还是简单用户,因此我将数据存储在特定表中,但登录数据存储在AspNetUsers中.

when a user registers on the site, he can choose whether he is a company or a simple user, so I store the data in a specific table but the login data are stored in AspNetUsers.

我如何在.net core中使用Identity处理它?<​​/p>

How can i handle it with Identity in .net core?

推荐答案

简单地从ApplicationUser继承,即:

public class CompanyUser : ApplicationUser {}

public class SimpleUser : ApplicationUser {}

默认情况下,EF将通过STI(单表继承)实现继承.本质上,ApplicationUser上的所有属性和所有派生都将进入ApplicationUser(即AspNetUsers)的表中.将添加一个Discriminator列,其中将保存已保存的实际类的名称.然后,EF将在查询时使用此实例化正确的类型.这种方法的一个缺点是,派生类 上的属性必须为空,因为并非每种使用类型都能够为其他用户类型的属性提供值.

By default, EF will implement inheritance via STI (single-table inheritance). Essentially, all properties on ApplicationUser and all derivations will all go into the table for ApplicationUser (i.e. AspNetUsers). A Discriminator column will be added that will house the name of the actual class that was saved. EF will then use this to instantiate the right type when querying. The one downside to this approach is that properties on derived classes must be nullable, since not every type of use would be able to provide values for properties on other user types.

如果愿意,可以按类型实现TPT或表.这将为您提供ApplicationUser的标准表,其中包含所有通用属性的列以及每个派生类型的表,其中仅包含在该派生类型上定义的属性,并返回到ApplicationUser表的外键.使用这种方法,您可以在派生类上具有不可为空的属性,但是查询用户将需要连接.如果您想走这条路,可以将Table属性添加到派生类中,并为其指定名称,或者使用OnModelCreating中的ToTable fluent config方法执行相同的操作

If you prefer, you can implement TPT or table per type. That will give you the standard table for ApplicationUser with columns for all common properties and a table for each derived type, containing only properties defined on that derived type and a foreign key back to the table for ApplicationUser. With this approach, you can have non-nullable properties on your derived classes, but querying a user will then require a join. If you want to go this route, you can either add the Table attribute to the derived class with the name you'd like its table to have, or use the ToTable fluent config method in OnModelCreating to do the same.

您需要牢记的一件事是,与用户一起使用的通用类(例如UserManager<TUser>)将需要针对您需要使用的每种类型进行实例化.例如,即使您执行new CompanyUser(),如果通过UserManager<ApplicationUser>实例保存它,也将被上传并保存为ApplicationUser实例.要实际保存CompanyUser实例,您需要使用UserManager<CompanyUser>实例.

One thing that you'll need to keep in mind is that generic classes that work with users such as UserManager<TUser> will need to be instantiate per type you need to work with. For example, even if you do new CompanyUser(), if you save it via an instance of UserManager<ApplicationUser> it will be upcast and saved as an instance of ApplicationUser. To actually save a CompanyUser instance, you'd need to use an instance of UserManager<CompanyUser> instead.

这篇关于如何使用身份和.net core 2创建两种类型的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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