feat: automatically apply EF Core database migrations on service startup across all services

This commit is contained in:
Ho Ngoc Hai
2026-02-28 00:51:35 +07:00
parent f521cc0a91
commit be86e48de6
19 changed files with 172 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -146,6 +147,14 @@ try
app.MapControllers();
// EN: Run the application / VI: Chạy ứng dụng
// EN: Auto-apply EF Core migrations on startup
// VI: Tự động áp dụng EF Core migrations khi khởi động
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<BookingContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)