feat: initialize manufacturing progress dashboard
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DongfangHydro.Dashboard.Api.Data;
|
||||
|
||||
public static class DatabaseInitializer
|
||||
{
|
||||
public static async Task InitializeAsync(WebApplication app)
|
||||
{
|
||||
var autoMigrate = app.Configuration.GetValue("Database:AutoMigrate", false);
|
||||
var seed = app.Configuration.GetValue("Database:Seed", false);
|
||||
if (!autoMigrate && !seed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await using var scope = app.Services.CreateAsyncScope();
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<DashboardDbContext>();
|
||||
|
||||
if (autoMigrate)
|
||||
{
|
||||
if (dbContext.Database.IsRelational())
|
||||
{
|
||||
await dbContext.Database.MigrateAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
await dbContext.Database.EnsureCreatedAsync();
|
||||
}
|
||||
}
|
||||
|
||||
if (seed)
|
||||
{
|
||||
await scope.ServiceProvider.GetRequiredService<DashboardSeeder>().SeedAsync(CancellationToken.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user