Identity Core 2 IdentityRole 是否缺少用户定义?

Is Identity Core 2 IdentityRole missing defintions for Users?

我是不是遗漏了什么,或者用户的定义是否已从 Identity Core 2 的 IdentityRole 中删除?

我正在使用 asp.net 核心 2,我需要计算每个角色的用户数。这在 Core 1 中使用以下标准代码工作得很好

public class ApplicationRoleController : Controller
{
    private readonly RoleManager<ApplicationRole> roleManager;    
    public ApplicationRoleController(RoleManager<ApplicationRole> roleManager)
    {
        this.roleManager = roleManager;
    }

    [HttpGet]
    public IActionResult Index()
    {
        List<ApplicationRoleListViewModel> model = new List<ApplicationRoleListViewModel>();
        model = roleManager.Roles.Select(r => new
        {
            RoleName = r.Name,
            Id = r.Id,
            Description = r.Description,
            NumberOfUsers = r.Users.Count
        }).ToList()
        .Select(r => new ApplicationRoleListViewModel
        {
            RoleName = r.RoleName,
            Id = r.Id,
            Description = r.Description,
            NumberOfUsers = r.NumberOfUsers
        }).ToList();

        return View(model);
    }

在我使用 Core 2 的应用程序中,行 NumberOfUsers = r.Users.Count,其中 r 来自 class ApplicationRole,错误是 "ApplicationRole does not contain a definition for Users" ApplicationRole 继承自 IdentityRole.

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System;

public class ApplicationRole : IdentityRole
{
    public string Description { get; set; }
    public DateTime CreatedDate { get; set; }
    public string IPAddress { get; set; }
}

你可以用这个

var admins = _userManager.GetUsersInRoleAsync("Admin").Result;
var number = admins.Count;

Core 2 的问题显然是他们放弃了导航属性 AspNetUsers AspNetUserRoles AspNetRoles。不知道为什么。老实说,这非常令人沮丧。据说你可以重新实现它们,但到目前为止我还没有运气。我在这里试图找到一种方法来列出所有用户及其所有角色。 我认为上面的代码是您所需要的。

同时 _userManager 在控制器中也是这样导入的

private readonly UserManager<ApplicationUser> _userManager;


    public MyControllerController(DBContext context, UserManager<ApplicationUser> userManager)
    {
        this.context = context;
        _userManager = userManager; ;
    }