AspNetRoles表未显示自定义字段

时间:2018-08-23 04:43:13

标签: asp.net asp.net-mvc entity-framework

我最近像这样扩展了我的AspNetRoles表:

public class AspNetRoles:IdentityRole
{
        public AspNetRoles() : base() { }
        public String Label { get; set; }
        public String ApplicationId { get; set; }
        public AspNetApplications Application { get; set; }

        public static readonly String SystemAdministrator = "SystemAdministrator";
 }

当我创建新角色时,效果很好。但是,当我尝试将其提取到这样的列表中时:

var data = dbContext.Roles.ToList();

并尝试进行如下搜索:

data = data.Where(u => u.Id.ToString().ToLower().Contains(Input.Search.ToLower())).ToList();

我无法访问ApplicationId列。我想念什么吗?

编辑:

我的dbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, AspNetRoles<string>, string>
{
    public virtual DbSet<AspNetUsersExtendedDetails> AspNetUsersExtendedDetails { get; set; }
    public virtual DbSet<AspNetApplications> AspNetApplications { get; set; }
    public virtual DbSet<AspNetEventLogs> AspNetEventLogs { get; set; }
    public virtual DbSet<AspNetRoles> AspNetRoles { get; set; }
    public ApplicationDbContext() : base("AppStudio")
    {

    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

我已经更新了dbContext,但现在显示此错误:'IdentityDbContext<ApplicationUser, AspNetRoles<string>, string>' does not contain a constructor that takes 2 arguments

2 个答案:

答案 0 :(得分:1)

您需要告知ASP.Net Identity您要使用的自定义角色表。

编辑:由于默认的IdentityRole实现使用string作为PK,因此可以省略该类型。只要在ASP.Net Identity版本2上进行进一步检查,一旦您指定了自定义IdentityRole类,该类声明就需要包含所有类型。

这意味着您需要这样声明ApplictionDbContext

public class ApplicationDbContext: IdentityDbContext<ApplicationUser, AspNetRoles, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
    public ApplicationDbContext() : base("AppStudio")
    {
        //note: before this change, if you included the 
        //throwIfV1Schema parameter in the constructor, 
        //it needs to be removed.
    }

    //implementation 
}

请注意,这假定用户表的主键是string。如果不是这种情况,请替换为适用的类型(例如Guid)。

答案 1 :(得分:0)

您必须将AspNetRoles添加到IdentityDbContext中。

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, AspNetRoles, string>
{
    public virtual DbSet<AspNetUsersExtendedDetails> AspNetUsersExtendedDetails { get; set; }
    public virtual DbSet<AspNetApplications> AspNetApplications { get; set; }
    public virtual DbSet<AspNetEventLogs> AspNetEventLogs { get; set; }
    public ApplicationDbContext() : base("AppStudio", throwIfV1Schema: false)
    {

    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}