0

In which module do I do the seeding of the db? I want to add roles and users if they do not exist yet. In NET Core I would use the startup file in this way:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        using (IServiceScope serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
        {

             var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
            var roleManager = serviceScope.ServiceProvider.GetService<RoleManager<IdentityRole>>();
            var userManager = serviceScope.ServiceProvider.GetService<UserManager<User>>();

            DbSeeder.Seed(Configuration, dbContext, roleManager, userManager);

        }
      }

and in my class DBSeeder:

public static class DbSeeder
{
    public static void Seed(IConfiguration configuration,
        ApplicationDbContext dbContext,
        RoleManager<IdentityRole> roleManager,
        UserManager<User> userManager)
    {
        if (!dbContext.Users.Any()) CreateUsers(configuration, dbContext, roleManager, userManager).GetAwaiter().GetResult();
    }

    private static async Task CreateUsers(IConfiguration configuration,
                                          ApplicationDbContext dbContext,
                                          RoleManager<IdentityRole> roleManager,
                                          UserManager<User> userManager)
    {

        string role_Administrator = "Administrator";
        string role_RegisteredUser = "RegisteredUser";

        if (!await roleManager.RoleExistsAsync(role_Administrator))
        {
            await roleManager.CreateAsync(new IdentityRole(role_Administrator));
        }
        if (!await roleManager.RoleExistsAsync(role_RegisteredUser))
        {
            await roleManager.CreateAsync(new IdentityRole(role_RegisteredUser));
        }

        var user_Admin = new User()
        {
            SecurityStamp = Guid.NewGuid().ToString(),
            UserName = "Admin",
            Email = configuration["ContactUs"],
            DisplayName = "Admin",
            EmailConfirmed = true
        };
        if (await userManager.FindByNameAsync(user_Admin.UserName) == null)
        {
            await userManager.CreateAsync(user_Admin, "Pass4Admin");
            await userManager.AddToRoleAsync(user_Admin, role_RegisteredUser);
            await userManager.AddToRoleAsync(user_Admin, role_Administrator);
        }


        await dbContext.SaveChangesAsync();
    }
user1238784
  • 2,250
  • 3
  • 22
  • 41
  • With Asp Mvc you have can create a seperate class for your initializer, then you set this on the dbcontext. There is a related question on stackoverflow which gives you an idea of the steps you can follow: https://stackoverflow.com/questions/6368553/entity-framework-database-setinitializer-simply-not-working – allan Feb 24 '20 at 14:12
  • I read that answer. But How do I access the dbContext, the UserManager, the RoleManager in my initializer class – user1238784 Feb 24 '20 at 15:10

0 Answers0