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

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

问题描述

有很多的困惑,似乎周围IdentityDbContext。

There is a lot of confusion it seems around 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-β1有一个用户和角色的项目,但我本来期望的实际表可用。 为什么不呢?如果我想要去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标识会消失,如果它是像实体框架的任何数据库上下文处理。

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.

推荐答案

的的用户 ApplicationDbContext 角色属性映射到 AspNetUsers AspNetRoles 表,实体的其余部分(声明登录的UserRole )通过导航属性自动映射。据我所知,与ASPNET表名prefixing在 ApplicationDbContext 唯一的自定义映射,其他一切都只是实体框架code首先约定。

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 实体导航属性访问用户的角色,索赔和登录(从用户 DbSet )。如果您想直接查询他们,明确地将其添加为 DbSet S于上下文...

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 2.0身份自曝用户角色 的IQueryable S于管理器类为方便起见,但它不提供在什么是可从的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身份IdentityDbContext黑盒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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