ASP.NET无法访问“角色”表中的新字段

时间:2018-08-25 17:13:12

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

我刚刚像这样扩展了AspNetRoles表:

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

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

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

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不会出现问题。只有当我尝试从中获取数据时,我才会遇到问题。

1 个答案:

答案 0 :(得分:0)

听起来很愚蠢,但是您可以检查是否已经在另一个名称空间中定义了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();