为什么 Asp.Net Identity IdentityDbContext 是一个黑盒? [英] Why is Asp.Net Identity IdentityDbContext a Black-Box?

查看:28
本文介绍了为什么 Asp.Net Identity IdentityDbContext 是一个黑盒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IdentityDbContext 似乎有很多混乱.

如果我们在应用程序中创建两个数据库上下文,一个用于身份,一个用于自定义业务数据,则身份数据库上下文继承自 IdentityDbContext,而我们的自定义业务数据继承自 DbContext.

If we create two Database Contexts in our application, one for Identity and one for our custom business data, the Identity Database Context inherits from IdentityDbContext while our custom business data inherits from DbContext.

那么让我们将以下内容添加到控制器中:

So let's add the following to a controller:

private MyDbContext db = new MyDbContext();
private ApplicationDbContext identityDb = new ApplicationDbContext();

控制器中的索引方法如下:

And the following to an Index method in the controller:

var thingsInMyBusinessDb = db.Things.ToList();
var usersInIndentityDb = identityDb.AspNetUsers.ToList(); // THIS WILL HAVE AN ERROR
var roles = identityDb.AspNetRoles.ToList(); // ERROR

您还会注意到身份数据库中的表不可用.这是为什么?

You will also notice that the tables in the Indentity Database are not available. Why is this?

目前从 2.0.0-beta1 开始,有一个用户和角色项目,但我希望实际的表可用.为什么不呢?如果我想访问 AspNetUserRoles 怎么办

Currently as of 2.0.0-beta1 there is a Users and Roles items, but I would have expected the actual tables to be available. And why not? What if I wanted to get to AspNetUserRoles

如果将 Asp.Net Identity 当作实体框架中的任何数据库上下文来处理,它的许多混乱和问题似乎都会消失.

Sure seems that a lot of confusion and issues with Asp.Net Identity would go away if it were treated like any database context in Entity Framework.

推荐答案

ApplicationDbContextUsersRoles 属性被映射到AspNetUsersAspNetRoles 表,以及其余的实体(ClaimsLoginsUserRolescode>) 通过导航属性自动映射.据我所知,带有AspNet"的表名前缀是 ApplicationDbContext 中唯一的自定义映射,其他一切都只是实体框架代码优先约定.

The ApplicationDbContext's Users and Roles properties are mapped to the AspNetUsers and AspNetRoles tables, and the rest of the entities (Claims, Logins, UserRoles) are mapped automatically via navigation properties. As far as I know, the prefixing of table names with "AspNet" are the only custom mappings in ApplicationDbContext, everything else is just Entity Framework Code First conventions.

如果您需要通过 ApplicationDbContext 直接访问表,您可以这样做...

If you need direct access to the tables via the ApplicationDbContext, you can do so like this...

using (var context = new ApplicationDbContext())
{
    var users = context.Users.Include(u => u.Claims)
                             .Include(u => u.Logins)
                             .Include(u => u.Roles)
                             .ToList();

    var roles = context.Roles.ToList();
}

您可以通过 IdentityUser 实体(来自 Users DbSet)上的导航属性访问用户的角色、声明和登录信息.如果您想直接查询它们,请在上下文中将它们显式添加为 DbSets...

You can access a user's roles, claims, and logins via navigation properties on the IdentityUser entity (from the Users DbSet). If you want to query them directly, add them explicitly as DbSets on the context...

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<IdentityUserRole> UserRoles { get; set; }
    public DbSet<IdentityUserClaim> Claims { get; set; }
    public DbSet<IdentityUserLogin> Logins { get; set; }

}

然后像这样查询它们...

And query them like this...

var claims = context.Claims.ToList();
var userRoles = context.UserRoles.ToList();
var logins = context.Logins.ToList();

ASP.NET Identity 2.0 为方便起见在 Manager 类上公开 UsersRoles IQueryables,但它不提供任何添加的DbContext 中可用的功能.

ASP.NET Identity 2.0 exposes Users and Roles IQueryables on the Manager classes for convenience, but it doesn't provide any added functionality over what was available from the DbContext.

这篇关于为什么 Asp.Net Identity IdentityDbContext 是一个黑盒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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