AspNetCore 2.0 Identity - Проблемы с внедрением RoleManager

Мне нужно создать операции CRUD для РОЛИ.

Я получаю следующую ошибку:

«Невозможно разрешить службу для типа 'Microsoft.AspNetCore.Identity.RoleManager`»

Итак, как я могу ввести roleManager?

Я использую asp net core 2.0 + identity 2.2.1

Класс ApplicationUser

 public class ApplicationUser : IdentityUser
    {
        [Key]
        public override string Id { get; set; }
        public bool Type { get; set; }
    }

Теперь в Startup.cs

        services.AddIdentity<ApplicationUser, IdentityRole<int>>()
        .AddUserStore<UserStore<ApplicationUser, IdentityRole<int>, ApplicationDbContext, int>>()
        .AddRoleStore<RoleStore<IdentityRole<int>, ApplicationDbContext, int>>()
        .AddDefaultTokenProviders();

Контроллер

private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityUser> _roleManager;

public RolesController(UserManager<ApplicationUser> userManager, RoleManager<IdentityUser> roleManager)
{
    _userManager = userManager;
    _roleManager = roleManager; 
}


public IActionResult Index()
{
    return View(_roleManager.Roles);
}

Итак, я получаю сообщение об ошибке: «Невозможно разрешить службу для типа Microsoft.AspNetCore.Identity.RoleManager».


person Fabio Alves    schedule 27.10.2017    source источник


Ответы (1)


Вы должны использовать .AddRoleManager ‹> вместо AddRoleStore‹>. Или и то, и другое, если вы хотите создать новые роли. Если вы не используете собственные магазины, попробуйте: AddEntityFrameworkStores

services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<YourContext>()
person Charly Guirao    schedule 14.02.2018