Asp.Net Identity - IdentityDbContext <tuser>导致添加到AspNetRoles表的字段出现问题</tuser>

时间:2014-02-26 19:21:23

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

IdentityDbContext导致添加到AspNetRoles

的属性/字段出现问题

我认为问题在于IdentityDbContext采用IdentityUser类型:

public class MyIdentityDb : IdentityDbContext<ApplicationUser>
{
    public IdentityDb()
        : base("IdentityDb")
    {
    }
}

但让我解释一下......

通过向继承自IdentityUser的ApplicationUser类添加属性,我们可以向AspNetUsers表添加字段,这很棒。例如:

public class ApplicationUser : IdentityUser
    {
        [Required]
        [StringLength(50)]
        public string FirstName { get; set; }

        [Required]
        [StringLength(50)]
        public string LastName { get; set; }
    }

很自然,我们可以通过向继承自IdentityRole的ApplicationRole类添加属性来向AspNetRoles表添加字段。例如:

public class ApplicationRole : IdentityRole
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }
}

完美的作品。我们可以在数据库中看到该字段。我们可以添加数据。例如:

RoleManager<ApplicationRole> roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(new MyIdentityDb()));

var role = new ApplicationRole() { Name = name, ProperName = propername };
var result = await roleManager.CreateAsync(role);

但是现在我们在尝试获取数据时遇到了问题。例如:

我们的ViewModel:

public class IndexViewModel
{
    public IList<ApplicationUser> Users { get; set; }
    public IList<ApplicationRole> Roles { get; set; }
}

在我们的控制器上:

private MyIdentityDb myIdentityDb = new MyIdentityDb();

我们的控制器上的索引方法:

public ViewResult Index(int? page)
{
     return View(new IndexViewModel
     {
          Users = myIdentityDb.Users.ToList(),
          Roles = myIdentityDb.Roles.ToList()
     });
}

错误发生在“myIdentityDb.Roles.ToList()”上,并显示“无法隐式转换类型System.Collection.Generic.List<Microsoft.AspNet.Identity.EntityFramework.IdentityRole> to System.Collections.Generic.IList<MyApp.Models.ApplicationRole> ...

当然,我们可以将ViewModel更改为使用类型IdentityRole,如下例所示,但是我们无法访问AspNetRoles表中的新“ProperName”字段:

public class IndexViewModel
{
    public IList<ApplicationUser> Users { get; set; }
    public IList<IdentityRole> Roles { get; set; }
}

因此我们可以尝试创建另一个Db类并将其传递给IdentityRole类型而不是IdentityUser:

public class MyIdentityDb : IdentityDbContext<ApplicationUser>
{
    public MyIdentityDb()
        : base("MyIdentityDb")
    {
    }
}

public class MyIdentityRolesDb : IdentityDbContext<ApplicationRole>
{
    public MyIdentityRolesDb()
        : base("MyIdentityDb")
    {
    }
}

更改我们的控制器:

private MyIdentityDb myIdentityDb = new MyIdentityDb();
private MyIdentityRolesDb myIdentityRolesDb = new MyIdentityRolesDb();

在我们的控制器上更改我们的Index方法:

public ViewResult Index(int? page)
{
     return View(new IndexViewModel
     {
          Users = myIdentityDb.Users.ToList(),
          Roles = myIdentityRolesDb.Roles.ToList()
     });
}

但我们最终遇到同样的问题; IdentityDbContext采用IdentityUser类型的事实。

我们如何获取自定义字段/属性的角色列表?

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

因此,如果您升级到2.0.0 beta软件包,您应该可以直接从角色管理器获取IQueryable的ApplicationRole:

roleManager.Roles

因此,您不必下载到DB上下文,这是1.0版本中的限制,已在2.0版本中修复。