DEV Community

Mahir Xalilov
Mahir Xalilov

Posted on

Database seeder

public static class DatabaseSeeder
{
public static async Task SeedData(IServiceProvider provider)
{
var userManager = provider.GetRequiredService>();
var roleManager = provider.GetRequiredService>();
var db = provider.GetRequiredService();

    db.Database.Migrate();

    string[] roles = { "Admin", "User" };
    foreach ( var role in roles )
    {
        if (!await roleManager.RoleExistsAsync(role))
        {
            await roleManager.CreateAsync(new IdentityRole(role));
        }
    }

    string email = "admin@gmail.com";
    string userName = "admin";
    string password = "Admin123!";
    if (await userManager.FindByEmailAsync(email) is null)
    {
        IdentityUser user = new IdentityUser
        {
            Email = email,
            UserName = userName,
            EmailConfirmed = true,
        };
        IdentityResult result = await userManager.CreateAsync(user,password);
        if (result.Succeeded)
        {
            await userManager.AddToRoleAsync(user,"Admin");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)