chore 重构数据库迁移逻辑并优化实体配置

This commit is contained in:
2025-10-16 22:03:06 +08:00
parent 69d2b460b6
commit 89c8236f99
8 changed files with 51 additions and 43 deletions

View File

@ -71,40 +71,38 @@ public abstract class PendingEfCoreMigrationsChecker<TDbContext> : ITransientDep
protected virtual async Task LockAndApplyDatabaseMigrationsAsync()
{
await using (var handle = await DistributedLockProvider.TryAcquireAsync("Migration_" + DatabaseName))
await using var handle = await DistributedLockProvider.TryAcquireAsync("Migration_" + DatabaseName);
Log.Information($"Lock is acquired for db migration and seeding on database named: {DatabaseName}...");
if (handle is null)
{
Log.Information($"Lock is acquired for db migration and seeding on database named: {DatabaseName}...");
Log.Information($"Handle is null because of the locking for : {DatabaseName}");
return;
}
if (handle is null)
using (CurrentTenant.Change(null))
{
// Create database tables if needed
using (var uow = UnitOfWorkManager.Begin(true, false))
{
Log.Information($"Handle is null because of the locking for : {DatabaseName}");
return;
}
var dbContext = ServiceProvider.GetRequiredService<TDbContext>();
using (CurrentTenant.Change(null))
{
// Create database tables if needed
using (var uow = UnitOfWorkManager.Begin(true, false))
var pendingMigrations = await dbContext
.Database
.GetPendingMigrationsAsync();
if (pendingMigrations.Any())
{
var dbContext = ServiceProvider.GetRequiredService<TDbContext>();
var pendingMigrations = await dbContext
.Database
.GetPendingMigrationsAsync();
if (pendingMigrations.Any())
{
await dbContext.Database.MigrateAsync();
}
await uow.CompleteAsync();
await dbContext.Database.MigrateAsync();
}
await ServiceProvider.GetRequiredService<IDataSeeder>()
.SeedAsync();
await uow.CompleteAsync();
}
Log.Information($"Lock is released for db migration and seeding on database named: {DatabaseName}...");
await ServiceProvider.GetRequiredService<IDataSeeder>()
.SeedAsync();
}
Log.Information($"Lock is released for db migration and seeding on database named: {DatabaseName}...");
}
}