ASP.NET Core 2 - Identity - DI errors with custom Roles

前端 未结 1 458
花落未央
花落未央 2021-01-07 21:39

I got this code in my Startup.cs:

 services.AddDbContext(options =>
            options.UseSqlServer(Configuration.GetConnecti         


        
相关标签:
1条回答
  • 2021-01-07 22:10

    You have to create the IServiceScope on your own.

    To do this you have to replace

    var roleManager = serviceProvider.GetService<RoleManager<ApplicationRole>>();
    

    with

    using (IServiceScope scope = app.ApplicationServices.CreateScope()) {
        RoleManager<IdentityRole> roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
    
        // Seed database code goes here
    }
    
    0 讨论(0)
提交回复
热议问题