Auto apply EF migrations on startup
All checks were successful
Build Docker Images / build-and-push (push) Successful in 1m25s
All checks were successful
Build Docker Images / build-and-push (push) Successful in 1m25s
This commit is contained in:
@@ -83,6 +83,33 @@ builder.Services.AddCors(options =>
|
|||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Auto-apply migrations on startup
|
||||||
|
using (var scope = app.Services.CreateScope())
|
||||||
|
{
|
||||||
|
var db = scope.ServiceProvider.GetRequiredService<BimAIDbContext>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var pending = await db.Database.GetPendingMigrationsAsync();
|
||||||
|
if (pending.Any())
|
||||||
|
{
|
||||||
|
app.Logger.LogWarning("Applying {Count} pending migrations: {List}",
|
||||||
|
pending.Count(), string.Join(", ", pending));
|
||||||
|
await db.Database.MigrateAsync();
|
||||||
|
app.Logger.LogInformation("Migrations applied successfully.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
app.Logger.LogInformation("No pending migrations.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
app.Logger.LogCritical(ex, "Migration failed - application will not start.");
|
||||||
|
throw; // stop startup
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
|
|||||||
Reference in New Issue
Block a user