【问题标题】:Asp.Net Identity - IdentityDbContext<TUser> causing problems for fields added to AspNetRoles tableAsp.Net Identity - IdentityDbContext<TUser> 导致添加到 AspNetRoles 表的字段出现问题
【发布时间】:2014-02-26 19:21:23
【问题描述】:

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

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

我们的视图模型:

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

在我们的控制器上:

private MyIdentityDb myIdentityDb = new MyIdentityDb();

我们控制器上的 Index 方法:

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; to System.Collections.Generic.IList&lt;MyApp.Models.ApplicationRole&gt;...

当然,我们可以将 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 类型的事实。

有什么想法可以让我们使用自定义字段/属性获取角色列表吗?

【问题讨论】:

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


    【解决方案1】:

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

    roleManager.Roles
    

    所以您不必下拉到 DB 上下文,这是 1.0 中的一个限制,已在 2.0 版本中修复。

    【讨论】:

    • 很高兴看到正在开发的功能。对于看到这一点的任何人,我使用的是 ApplicationRole 而不是 IdentityRole,因为我在 AspNetRoles 表中添加了字段。如果您只需要获取角色,那么您可以使用 IdentityRole。公共 RoleManager RoleManager { 获取;私人套装; }
    • Hao 为什么数据库上下文不像其他数据库上下文那样对待?看看stackoverflow.com/q/22105583/1980270
    • @HaoKung 我遇到了一些奇怪的错误。 UserId not found.Plzz看看这个堆栈溢出问题---stackoverflow.com/questions/33455346/…
    【解决方案2】:

    【讨论】:

    • 希望 Asp.Net-Identity 人员能够使 IdentityDbContext 像 DbContext 一样工作,并且只公开所有表和所有字段。很高兴我们可以如此轻松地向表格中添加字段——如果它们易于使用会更好!
    • @Pranav 您介意添加一些摘要吗?仅链接的答案价值有限,因为链接迟早会断开。
    • @Pranav 虽然文章最后确实公开了添加到 AspNetRoles 表中的字段,但它使用了一个具有“RoleManager”的新类“IdentityManager”。它没有解释为什么或如何(从我上面的代码)我们可以使用 {myIdentityDb.Users.ToList();} 访问在 AspNetUsers 上创建的新字段,但不能使用 {myIdentityDb. Roles.ToList();}
    • @pranav rastogi plzz 看看这个 UserId not found 错误是 aspnet 身份框架中的错误还是问题-----stackoverflow.com/questions/33455346/…
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多