Asp.Net身份 - IdentityDbContext<&TUSER GT;对于领域造成的问题加入到AspNetRoles表 [英] Asp.Net Identity - IdentityDbContext<TUser> causing problems for fields added to AspNetRoles table

查看:231
本文介绍了Asp.Net身份 - IdentityDbContext<&TUSER GT;对于领域造成的问题加入到AspNetRoles表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这个问题是在这里与IdentityDbContext服用IdentityUser类型:

I believe the problem is here with IdentityDbContext taking an IdentityUser type:

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

我来解释一下...

这是美妙的,我们可以通过添加属性,从IdentityUser继承我们ApplicationUser类添加到AspNetUsers表中的字段。例如:

It is wonderful that we can add fields to the AspNetUsers table by adding properties to our ApplicationUser class that inherits from IdentityUser. Example:

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

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

那么自然,我们可以通过添加属性,从IdentityRole继承的ApplicationRole类添加到AspNetRoles表中的字段。例如:

So natural we can add fields to the AspNetRoles table by adding properties to an ApplicationRole class that inherits from IdentityRole. Example:

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

运行完美。我们可以看到在数据库中的字段。我们可以将数据添加到它。例如:

Works perfect. We can see the field in the database. We can add data to it. Example:

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);

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

But now we run into a problem when trying to get to the data. Example:

我们的视图模型:

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

在我们的控制器:

private MyIdentityDb myIdentityDb = new MyIdentityDb();

我们的我们的控制器Index方法:

Our Index method on our controller:

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

的错误是在myIdentityDb.Roles.ToList()中记载无法隐式转换类型 System.Collection.Generic.List&LT; Microsoft.AspNet.Identity.EntityFramework.IdentityRole&GT;到System.Collections.Generic.IList&LT; MyApp.Models.ApplicationRole&GT; ...

The error is on "myIdentityDb.Roles.ToList()" and reads "Cannot implicitly convert type System.Collection.Generic.List<Microsoft.AspNet.Identity.EntityFramework.IdentityRole> to System.Collections.Generic.IList<MyApp.Models.ApplicationRole>

当然,我们可以改变我们的视图模型使用类型IdentityRole像下面的例子,但我们不能让新的ProperName字段中AspNetRoles表:

Of course we could change our ViewModel to use type IdentityRole like the following example, but then we cannot get to the new "ProperName" field in the AspNetRoles table:

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

因此​​,我们可以尝试再创建一个DB类,并传递一个IdentityRole类型,而不是IdentityUser:

So we could try creating another Db class and pass it an IdentityRole type instead of 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();

和改变我们的指数方法,我们的控制器:

And change our Index method on our controller:

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

但我们最终同样的问题;事实IdentityDbContext接受一个I​​dentityUser型

But we end up with the same problem; the fact that IdentityDbContext takes an IdentityUser type.

任何想法,我们怎么能得到角色的列表,自定义字段/属性?

Any ideas how we can get a list of Roles with the custom fields/properties?

推荐答案

因此​​,如果您升级到2.0.0测试版软件包,你应该能够直接从角色管理器获得ApplicationRole的一个IQueryable:

So if you upgrade to the 2.0.0 beta packages, you should be able to get an IQueryable of ApplicationRole directly from the role manager:

roleManager.Roles

所以,你不必再下降到数据库的背景下,这是在1.0的限制已被固定在2.0版本。

So you don't have to drop down to the DB context, this was a limitation in 1.0 that has been fixed in the 2.0 release.

这篇关于Asp.Net身份 - IdentityDbContext&LT;&TUSER GT;对于领域造成的问题加入到AspNetRoles表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆