在asp.net mvc的5应用程序扩展asp.net身份 [英] Extending asp.net identity in asp.net mvc 5 application

查看:124
本文介绍了在asp.net mvc的5应用程序扩展asp.net身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在asp.net MVC 5应用程序。我使用的身份创建用户。我使用它第一次创建第一个MVC 5应用程序。我有两种类型的用户。我想这两种类型的用户的两个表。所以,第一个记录应aspnetuser表中插入,然后在相关的表格和aspnetuser表应与表链接,这样我可以访问它们。我创建的模型两种类型的用户。我怎样才能模型的身份联系起来呢?

I am working on asp.net MVC 5 application. I am using Identity for creating users. I am using it first time and creating first mvc 5 application. I have two types of users. I want two tables for both types of users. So first record should be inserted in aspnetuser table and then in related tables and aspnetuser table should be linked with both tables so that i can access them. I have created models for both types of users. How can i link them with identity in model ?

请提示

推荐答案

嗯,首先,你才能真正拥有一个用户,至少在的东西不认同方面。你也可以继承的单个用户创建其他类型,虽然。所以,从本质上讲,你必须像一个设置:

Well, first, you can only truly have one user, at least in terms of the stuff Identity does. You can subclass that single user to create other types, though. So, essentially, you'd have a setup like:

public class ApplicationUser : IdentityUser
{
    ...
}

public class FooUser : ApplicationUser
{
    ...
}

public class BarUser : ApplicationUser
{
    ...
}

public class BazUser : ApplicationUser
{
    ...
}

在换句话说,只有一个类直接从 IdentityUser 继承,而你的所有各类将从一个类继承(在这种情况下, ApplicationUser )。

In other words, just one class inherits directly from IdentityUser, while all your various types will inherit from that one class (in this case, ApplicationUser).

在默认情况下,实体框架通过STI或单表继承处理继承。这是如何工作是,你只有一个表 AspNetUsers (用于用户身份的默认表名),你的子类添加将作为在该列的任何属性表。这意味着,所有的子类的属性必须是空的,因为如果不同的子类,或者只是基本 ApplicationUser 类持久保存该属性不会被设置。

By default, Entity Framework handles inheritance via STI or Single Table Inheritance. How this works is that you'll have just one table AspNetUsers (the default table name for the Identity user), and any properties your subclasses add will be added as columns on that table. This means that all your subclass properties have to be nullable, since if a different subclass or just the base ApplicationUser class are persisted that property wouldn't be set.

如果这是不能接受的,还有其他的继承策略就可以实现,即TPT(表,每型)和TPC(表每个具体类型)。在TPT,基地,共享属性全力以赴在一个表,然后将每个亚型有它添加的属性只是列了自己的表。在TPC,每个类都得到它的一切自己的表,所以每个单独的亚型会对所有的标识列的补充。我不相信这个特别的选择将是与Identity兼容,不过,或者最起码这将是非常沉重的负担,因为所有的东西像角色和债权之间的外键关系必须被复制为每个表。即使你能砍的身份,使这项工作,你会失去与所有用户做任何事情的任何凝聚力的方式,无论哪种类型,在一次。

If that's not acceptable, there's other inheritance strategies you can implement, namely TPT (Table-Per-Type) and TPC (Table-Per-Concrete Type). In TPT, the base, shared properties go all in one table and then each subtype has its own table with just columns for the properties it adds. In TPC, every class gets it's own table for everything, so each of your individual subtypes would have all of the Identity columns added. I don't believe this particular option would be compatible with Identity, though, or at the very least it would be highly burdensome as all the foreign key relationships between things like roles and claims would have to be duplicated for each table. Even if you could hack Identity to make that work, you'd lose any cohesive way of doing anything with all users, regardless of type, at once.

基本上,你的选择是默认的,也被称为TPH(表 - 每等级),或TPT。然而,TPT是效率较低,因为它本质上需要为每个查询联接只是为了获得完整的实例。如果你确实需要对你的子类不可为空的特性,它可能是一个合适的选择。但是,请记住,物业仅需要在数据库级空。如果你有TPH去,你仍然可以要求这些属性通过在应用程序中确认具有价值。

Basically, your options are the default, also known as TPH (Table-Per-Hierarchy), or TPT. However, TPT is less efficient because it essentially requires a join for every query just to get the full instance. If you absolutely need to have non-nullable properties on your subclass, it might be an appropriate option. However, remember that the property only needs to be nullable at the database-level. If you go with TPH, you can still require that those properties have values via validation in the application.

这篇关于在asp.net mvc的5应用程序扩展asp.net身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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