Files
StatiKosev/Data/DbSeeder.cs
T

22 lines
699 B
C#
Raw Normal View History

2026-10-28 13:23:35 +02:00
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace StatiKosev.Data;
public static class DbSeeder
{
public const string UserRole = "User";
2026-10-28 14:22:27 +02:00
//applies pending migrations and creates the role every registered visitor gets
public static async Task SeedAsync(IServiceProvider services)
2026-10-28 13:23:35 +02:00
{
using var scope = services.CreateScope();
var sp = scope.ServiceProvider;
await sp.GetRequiredService<AppDbContext>().Database.MigrateAsync();
var roles = sp.GetRequiredService<RoleManager<IdentityRole>>();
2026-10-28 14:22:27 +02:00
if (!await roles.RoleExistsAsync(UserRole))
await roles.CreateAsync(new IdentityRole(UserRole));
2026-10-28 13:23:35 +02:00
}
}