首页 > 解决方案 > ASP.NET 无法访问角色表中的新字段

问题描述

我刚刚扩展了我的 AspNetRoles 表,如下所示:

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

当我添加我的迁移并更新数据库时,出现了 ApplicationId 和一个鉴别器列。

但是,当我需要访问该表中的数据时,我似乎无法访问新字段:

List<ApplicationRole> data = (from ar in dbContext.Roles
     join a in dbContext.AspNetApplications
     on ar.ApplicationId equals a.Id
     select new ApplicationRole
     {
         Id = ar.Id,
         Name = ar.Name
         ApplicationId = (Unable to access this section)
     }).ToList();

我已经尝试按照本教程http://johnatten.com/2014/06/22/asp-net-identity-2-0-customizing-users-and-roles/#Extending-Identity-Role并添加了一个新的我的 IdentityConfig 中的 ApplicationRoleManager

public class ApplicationRoleManager : RoleManager<ApplicationRole>
    {
        public ApplicationRoleManager(IRoleStore<ApplicationRole, string> roleStore): base(roleStore)
        {
        }
        public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
        {
            return new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));
        }
    }

当我为我的数据库播种时,ApplicationId 被填满没有问题。只有当我尝试从中获取数据时才会遇到问题。

标签: asp.netasp.net-mvcentity-frameworkasp.net-mvc-5

解决方案


听起来很傻,但是你能检查一下你是否已经在另一个命名空间中定义了 ApplicationRole 类,也许你想要填充的那个来自另一个你没有定义 ApplicationId 的命名空间

select new ApplicationRole
 {
     Id = ar.Id,
     Name = ar.Name , //or maybe because you do not have a comma after ar.Name
     ApplicationId = (Unable to access this section)
 }).ToList();

推荐阅读