为什么ASP.NET身份的用户和角色不会出现在Db上下文中

时间:2014-10-06 22:26:06

标签: c# asp.net linq entity-framework asp.net-mvc-5

我正在使用ASP.NET Identity 2.0,根据我的理解使用实体框架。我很好奇为什么在DB上下文中没有设置DbSet<ApplicationUser>属性?如果它们没有在上下文中按字面指定,它仍然如何使用相同的上下文。这些是否适用于基类?

我想写一个来自ApplicationUser - &gt;的Linq查询ApplicationRoles - &gt; ApplicationRole(这是一个自引用属性)并且想要使用Linq to Entities,但是我在Google上找不到很多内容,并希望在编写Linq查询之前确保我的上下文正确。

ApplicationUser.cs

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

ApplicationRole.cs

public class ApplicationRole : IdentityRole
{
    public ApplicationRole() : base() { }

    public ApplicationRole(string name)
        : base(name)
    { }

    public ApplicationRole(string name, string description)
        : base(name)
    {
        this.Description = description;
    }

    public string Description { get; set; }

    public virtual ApplicationRole ParentRole { get; set; }
}

1 个答案:

答案 0 :(得分:1)

我猜你有自己的DbContext,你只需要像这样扩展IdentityDbContext:

public class MyDbContext: IdentityDbContext<ApplicationUser>

一旦你扩展了这个,那么你应该在你的DbContext上提供Identity类(除了你数据库中已有的其他东西)。

相关问题