我最近扩展了我的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();
我无法访问"应用程序 ID"列。我错过了什么吗?
编辑:
我的数据库上下文
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();
}
}
我已经更新了我的数据库上下文,但现在它显示此错误:'IdentityDbContext<ApplicationUser, AspNetRoles<string>, string>' does not contain a constructor that takes 2 arguments
您需要告知 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
(。
您必须将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();
}
}