2017-10-27 10 views
3

私はROLESのCRUD操作を作成する必要があります。私は次のエラーを取得していますAspNetCore 2.0 ID - RoleManagerの注入に関する問題

「タイプのサービスを解決できません「Microsoft.AspNetCore.Identity.RoleManager`」

をので、どのように私はroleManagerを注入することができますか? Startup.cs

で今

私は、ASPネットコア2.0 + IDを使用しています2.2.1

クラスApplicationUser

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

 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`のサービスを解決することができません。

答えて

0

AddRoleStore <>の代わりに.AddRoleManager <>を使用する必要があります。または、新しいロールを作成する場合は両方を選択します。 カスタムストアを使用していない場合は、次のように試してください:AddEntityFrameworkStores

services.AddIdentity<ApplicationUser, IdentityRole>() 
       .AddEntityFrameworkStores<YourContext>() 
関連する問題