using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; namespace Hcs.WebApp.Data { public class AppIdentityDbContext(DbContextOptions options) : IdentityDbContext(options) { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder .UseSeeding((context, _) => { var adminRole = context.Set().FirstOrDefault(x => x.Name == AppRole.ADMINISTRATOR_TYPE); if (adminRole == null) { adminRole = new AppRole() { Name = AppRole.ADMINISTRATOR_TYPE, NormalizedName = AppRole.ADMINISTRATOR_TYPE.Normalize() }; context.Set().Add(adminRole); } var operatorRole = context.Set().FirstOrDefault(x => x.Name == AppRole.OPERATOR_TYPE); if (operatorRole == null) { context.Set().Add(new AppRole() { Name = AppRole.OPERATOR_TYPE, NormalizedName = AppRole.OPERATOR_TYPE.Normalize() }); } var observerRole = context.Set().FirstOrDefault(x => x.Name == AppRole.OBSERVER_TYPE); if (observerRole == null) { context.Set().Add(new AppRole() { Name = AppRole.OBSERVER_TYPE, NormalizedName = AppRole.OBSERVER_TYPE.Normalize() }); } var adminUser = context.Set().FirstOrDefault(x => x.UserName == AppUser.ADMINISTRATOR_USER_NAME); var hasAdmin = adminUser != null; if (adminUser == null) { adminUser = new AppUser() { UserName = AppUser.ADMINISTRATOR_USER_NAME, NormalizedUserName = AppUser.ADMINISTRATOR_USER_NAME.Normalize(), LockoutEnabled = true }; var passwordHasher = context.GetService>(); adminUser.PasswordHash = passwordHasher.HashPassword(adminUser, AppUser.ADMINISTRATOR_PASSWORD); var entry = context.Set().Add(adminUser); } context.SaveChanges(); if (!hasAdmin) { context.Set>().Add(new IdentityUserRole() { UserId = adminUser.Id, RoleId = adminRole.Id }); } context.SaveChanges(); }) .UseAsyncSeeding(async (context, _, cancellationToken) => { var adminRole = await context.Set().FirstOrDefaultAsync(x => x.Name == AppRole.ADMINISTRATOR_TYPE, cancellationToken); if (adminRole == null) { adminRole = new AppRole() { Name = AppRole.ADMINISTRATOR_TYPE, NormalizedName = AppRole.ADMINISTRATOR_TYPE.Normalize() }; context.Set().Add(adminRole); } var operatorRole = await context.Set().FirstOrDefaultAsync(x => x.Name == AppRole.OPERATOR_TYPE, cancellationToken); if (operatorRole == null) { context.Set().Add(new AppRole() { Name = AppRole.OPERATOR_TYPE, NormalizedName = AppRole.OPERATOR_TYPE.Normalize() }); } var observerRole = await context.Set().FirstOrDefaultAsync(x => x.Name == AppRole.OBSERVER_TYPE, cancellationToken); if (observerRole == null) { context.Set().Add(new AppRole() { Name = AppRole.OBSERVER_TYPE, NormalizedName = AppRole.OBSERVER_TYPE.Normalize() }); } var adminUser = await context.Set().FirstOrDefaultAsync(x => x.UserName == AppUser.ADMINISTRATOR_USER_NAME, cancellationToken); var hasAdmin = adminUser != null; if (!hasAdmin) { adminUser = new AppUser() { UserName = AppUser.ADMINISTRATOR_USER_NAME, NormalizedUserName = AppUser.ADMINISTRATOR_USER_NAME.Normalize(), LockoutEnabled = true }; var passwordHasher = context.GetService>(); adminUser.PasswordHash = passwordHasher.HashPassword(adminUser, AppUser.ADMINISTRATOR_PASSWORD); var entry = context.Set().Add(adminUser); } await context.SaveChangesAsync(); if (!hasAdmin) { context.Set>().Add(new IdentityUserRole() { UserId = adminUser.Id, RoleId = adminRole.Id }); } await context.SaveChangesAsync(); }); base.OnConfiguring(optionsBuilder); } } }