2

I am trying to seed some roles to my identity db context, when I initially create the database.

For that I tried to implement the code like it was stated here: https://stackoverflow.com/a/29547994/985798

I tried this in the ConfigureServices-method inside my Startup-class: public void ConfigureServices(IServiceCollection services)

using this snippet:

var rolestore =
    new Microsoft.AspNetCore.Identity.EntityFrameworkCore.
        RoleStore<Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>(identityContext);

var roleManager = new Microsoft.AspNetCore.Identity.RoleManager
                  <Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>(rolestore);

//use rolemanager to insert roles etc.

The problem is, that it seems to not work in an fresh Core-project, created with Visual Studio 2017.

It gives me the following build error:

CS7036 There is no argument given that corresponds to the required formal parameter 'roleValidators' of 'RoleManager.RoleManager(IRoleStore, IEnumerable>, ILookupNormalizer, IdentityErrorDescriber, ILogger>, IHttpContextAccessor)'

Even if I use the other overload (with null values for the other parameters), the RoleManager seems to have no "Create" method anymore.

So, I am stuck at this point. What do I need to do? Has something changed in the ASP.NET MVC Core implementation of the rolemanager? Do I use something wrong?

Community
  • 1
  • 1
Jannik
  • 2,310
  • 6
  • 32
  • 61
  • After you get it compiled, it may prove a problem to get the Seed() executed. Afaik that's planned for EfCore 2.0 . – H H Apr 23 '17 at 16:42
  • What do you mean exactly? Running it inside the startup should work just fine, shouldn't it? I know, it might not be the best place, but I assume it should work. – Jannik Apr 23 '17 at 17:22
  • 2
    Instead of "newing" RoleManager try resolving it in `Configure` method. [Similar post](https://stackoverflow.com/questions/39934201/asp-net-core-identity-add-custom-user-roles-on-application-startup/39934793#39934793) and [MusicStore](https://github.com/aspnet/MusicStore/blob/1.0.0/src/MusicStore/Models/SampleData.cs#L22-L34) – tmg Apr 24 '17 at 07:21
  • 1
    Works for me. Like you said, you can use the IServiceCollection to retreave both your UserManager and your RoleManager. – Jannik Apr 25 '17 at 04:26

1 Answers1

4
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

public static class Seed
{
    public static void Initialize(IServiceProvider provider)
    {
        var _context = provider.GetRequiredService<ApplicationDbContext>();
        var userManager = provider.GetRequiredService<UserManager<ApplicationUser>>();
        var roleManager = provider.GetRequiredService<RoleManager<IdentityRole>>();


    }
}

Then below the majority of the code in the in the Configure() in startup.cs

Seed.Initialize(app.ApplicationServices);

HTH (hope that helps).

mvermef
  • 3,814
  • 1
  • 23
  • 36