0

dotnet core Authentication Documentation を読んで、いくつかの例を試してみました。新しいユーザーを作成できましたが、新しいロールの作成で問題が発生しました。RoleManager.CreateAsync を呼び出すと、新しいロールが作成されません。

ApplicationContext クラス:

  public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<string>, string>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {

        }
     }

startup.cs:

   services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration["AppSetting:DefaultConnection"]));

    services.AddIdentity<ApplicationUser, IdentityRole<string>>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

私のリポジトリ:

 private readonly UserManager<ApplicationUser> _userManager;
        private readonly SignInManager<ApplicationUser> _signManager;
        private readonly ApplicationDbContext _context;
        private readonly RoleManager<IdentityRole<string>> _roleManager;
        public AccountRespository(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signManager, ApplicationDbContext context, RoleManager<IdentityRole<string>> roleManager)
        {
            _signManager = signManager;
            _userManager = userManager;
            _context = context;
            _roleManager = roleManager;
        }

再投稿の私の create Role メソッド

public async Task CreateRoles(string roleName)
        {
            await _roleManager.CreateAsync(new IdentityRole() { Name = "Admin" });
        }
4

1 に答える 1