【问题标题】:Why do Users and Roles for ASP.NET identity not appear in Db Context为什么 ASP.NET 标识的用户和角色不会出现在 Db 上下文中
【发布时间】:2014-12-01 06:42:11
【问题描述】:

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

我想编写一个从 ApplicationUser -> ApplicationRoles -> ApplicationRole (这是一个自引用属性)的 Linq 查询,并想为此使用 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; }
}

【问题讨论】:

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


    【解决方案1】:

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

    public class MyDbContext: IdentityDbContext<ApplicationUser>
    

    一旦你扩展了它,那么你应该在你的 DbContext 上拥有 Identity 类(除了你的数据库中已经存在的其他东西)。

    【讨论】:

      猜你喜欢
      • 2010-12-12
      • 2010-10-31
      • 1970-01-01
      • 2016-04-25
      • 2016-08-26
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多