Skip to content

Instantly share code, notes, and snippets.

@vancodocton
Created May 7, 2022 12:32
Show Gist options
  • Save vancodocton/6fea08fb95f100802abe794bc8252f82 to your computer and use it in GitHub Desktop.
Save vancodocton/6fea08fb95f100802abe794bc8252f82 to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using WebApp.Models;
namespace WebApp.Data
{
public class SeedData
{
public async static Task Initializer(IServiceProvider serviceProvider, IConfiguration configuration)
{
await SeedRoleData(serviceProvider.GetRequiredService<RoleManager<IdentityRole>>());
await SeedDefaultAccountData(serviceProvider.GetRequiredService<UserManager<ApplicationUser>>());
await SeedDepartmentData(serviceProvider.GetRequiredService<ApplicationDbContext>());
await SeedCategoryData(serviceProvider.GetRequiredService<ApplicationDbContext>());
}
private static async Task SeedRoleData(RoleManager<IdentityRole> roleManager)
{
var roles = new List<string>()
{
Role.Admin, Role.Manager, Role.Coordinator, Role.Staff
};
foreach (var role in roles)
{
if (await roleManager.RoleExistsAsync(role) == false)
{
await roleManager.CreateAsync(new IdentityRole(role));
}
}
}
private static async Task SeedDefaultAccountData(UserManager<ApplicationUser> userManager)
{
var admin = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]",
EmailConfirmed = true,
};
var manager = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]",
EmailConfirmed = true,
};
string password = "asd@12E";
// Seed admin account
if (await userManager.FindByEmailAsync(admin.Email) == null)
{
var result = await userManager.CreateAsync(admin, password);
if (result.Succeeded)
await userManager.AddToRoleAsync(admin, Role.Admin);
}
// Seed manager account
if (await userManager.FindByEmailAsync(manager.Email) == null)
{
var result = await userManager.CreateAsync(manager, password);
if (result.Succeeded)
await userManager.AddToRoleAsync(manager, Role.Manager);
}
}
private static async Task SeedDepartmentData(ApplicationDbContext context)
{
var departments = new List<Department>()
{
new Department() { Name="Academic" },
new Department() { Name="Support" },
};
foreach (var department in departments)
{
if (!await context.Department.AnyAsync(d => d.Name == department.Name))
await context.Department.AddAsync(department);
}
await context.SaveChangesAsync();
}
private static async Task SeedCategoryData(ApplicationDbContext context)
{
var categorys = new List<Category>()
{
new Category() { Name="Things You Get in the Mail" },
new Category() { Name="Things You Do Every Day" },
new Category() { Name="Things You Store Items In" },
};
foreach (var category in categorys)
{
if (!await context.Category.AnyAsync(d => d.Name == category.Name))
await context.Category.AddAsync(category);
}
await context.SaveChangesAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment