autobus-api_old/AutobusApi.Infrastructure/DbInitializer.cs
2023-11-15 19:00:34 +02:00

60 lines
1.8 KiB
C#

using AutobusApi.Domain.Enums;
using AutobusApi.Infrastructure.Identity;
using AutoubsApi.Infrastructure.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace AutobusApi.Infrastructure;
public static class DbInitializer
{
public static void Initialize(ApplicationDbContext dbContext, ApplicationIdentityDbContext identityDbContext)
{
if (dbContext.Database.IsRelational())
{
var domainAppliedMigrations = dbContext.Database.GetAppliedMigrations();
var identityAppliedMigrations = identityDbContext.Database.GetAppliedMigrations();
if (domainAppliedMigrations.Count() == 0)
{
dbContext.Database.Migrate();
InitializeDomain(dbContext);
}
if (identityAppliedMigrations.Count() == 0)
{
identityDbContext.Database.Migrate();
InitializeIdentity(identityDbContext);
}
}
else
{
dbContext.Database.EnsureCreated();
InitializeDomain(dbContext);
identityDbContext.Database.EnsureCreated();
InitializeIdentity(identityDbContext);
}
}
private static void InitializeDomain(ApplicationDbContext dbContext)
{
}
private static void InitializeIdentity(ApplicationIdentityDbContext identityDbContext)
{
foreach (var role in Enum.GetValues(typeof(IdentityRoles)).Cast<IdentityRoles>())
{
identityDbContext.Roles.Add(new IdentityRole<int>
{
Name = role.ToString(),
NormalizedName = role.ToString().ToUpper(),
ConcurrencyStamp = Guid.NewGuid().ToString()
});
}
identityDbContext.SaveChanges();
}
}