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;
@@ -127,6 +128,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<AdsAnalyticsServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -127,6 +128,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<AdsBillingServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -127,6 +128,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<AdsManagerServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using AdsServingService.API.Application.Events;
using AdsServingService.API.Application.Services;
@@ -132,6 +133,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<AdsServingServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -127,6 +128,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<AdsTrackingServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

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)

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<CatalogContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -202,6 +203,14 @@ try
});
// 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<ChatServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

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<FnbContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -149,6 +150,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<InventoryContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -197,6 +198,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<MembershipServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -150,6 +151,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<MerchantServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -217,6 +218,14 @@ try
app.MapHub<MiningHub>("/hubs/mining");
// 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<MiningServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -147,6 +148,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<MissionDbContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using System.Data;
using Asp.Versioning;
using FluentValidation;
@@ -209,6 +210,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<OrderContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -191,6 +192,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<PromotionServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -153,6 +154,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<SocialServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,9 +1,11 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using Asp.Versioning.ApiExplorer;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
using StorageService.API.Application.Behaviors;
using StorageService.Infrastructure;
using StorageService.Infrastructure.Persistence;
using Serilog;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerGen;
@@ -181,6 +183,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<StorageServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)

View File

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Asp.Versioning;
using FluentValidation;
using Hellang.Middleware.ProblemDetails;
@@ -176,6 +177,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<WalletServiceContext>();
await dbContext.Database.MigrateAsync();
}
app.Run();
}
catch (Exception ex)