Get all users with a specific role in ASP.NET Core 2.0

六眼飞鱼酱① 提交于 2020-01-22 21:22:04

问题


I'm just getting started with porting a application from ASP.NET MVC 5 to ASP.NET Core 2.0 (with EF Core and Identity). All has been going smooth, but I've ran into a problem that I can't seem to find an answer to.

What I want to do is simply to fetch all users in a database, based on the role they are assigned (Admin, User, etc.).

This was simple in MVC 5 where I was just using:

_db.UserCompany.Where(x => x.User.Roles.Any(s => s.RoleId == adminRole.Id)

However, with .NET Core / Entity Framework Core I don't seem to be able to navigate through the User -> Role anymore.

So my question is if there is a already implemented way in Identity/EF Core to achieve what I want? Or is it necessary to implement a custom solution?

Thanks in advance!


回答1:


For me, User Manager was the answer

private readonly UserManager<ApplicationUser> _userManager;

_userManager.GetUsersInRoleAsync("myRole").Result


来源:https://stackoverflow.com/questions/46384698/get-all-users-with-a-specific-role-in-asp-net-core-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!