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

@ -1,9 +1,11 @@
using KonSoft.Admin.DbMigrations;
using KonSoft.Admin.EntityFrameworkCore; using KonSoft.Admin.EntityFrameworkCore;
using KonSoft.Shared.Hosting.AspNetCore; using KonSoft.Shared.Hosting.AspNetCore;
using KonSoft.Shared.Hosting.Microservices; using KonSoft.Shared.Hosting.Microservices;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.BackgroundJobs; using Volo.Abp.BackgroundJobs;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
@ -74,4 +76,10 @@ public class AdminHttpApiHostModule : AbpModule
app.UseAbpSerilogEnrichers(); app.UseAbpSerilogEnrichers();
app.UseConfiguredEndpoints(); app.UseConfiguredEndpoints();
} }
public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
{
await context.ServiceProvider.GetRequiredService<AdminPendingEfCoreMigrationsChecker>()
.CheckAndApplyDatabaseMigrationsAsync();
}
} }

View File

@ -1,6 +1,7 @@
using AutoMapper; using AutoMapper;
using KonSoft.Admin.Dtos; using KonSoft.Admin.Dtos;
using KonSoft.Admin.Entities; using KonSoft.Admin.Entities;
using KonSoft.Admin.ValueObjects;
namespace KonSoft.Admin; namespace KonSoft.Admin;

View File

@ -6,6 +6,7 @@ using KonSoft.Admin.Dtos;
using KonSoft.Admin.Entities; using KonSoft.Admin.Entities;
using KonSoft.Admin.IApplicationServices; using KonSoft.Admin.IApplicationServices;
using KonSoft.Admin.IRepositories; using KonSoft.Admin.IRepositories;
using KonSoft.Admin.ValueObjects;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;

View File

@ -1,5 +1,6 @@
using System; using System;
using KonSoft.Admin.Enums; using KonSoft.Admin.Enums;
using KonSoft.Admin.ValueObjects;
using Volo.Abp.Domain.Entities.Auditing; using Volo.Abp.Domain.Entities.Auditing;
namespace KonSoft.Admin.Entities; namespace KonSoft.Admin.Entities;

View File

@ -1,7 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using Volo.Abp.Domain.Values; using Volo.Abp.Domain.Values;
namespace KonSoft.Admin.Entities; namespace KonSoft.Admin.ValueObjects;
/// <summary> /// <summary>
/// 地址 /// 地址

View File

@ -1,4 +1,4 @@
using KonSoft.Admin.Entities; using KonSoft.Admin.EntityFrameworkCore.Configures;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore; using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore; using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
@ -41,6 +41,8 @@ public class AdminDbContext :
builder.ConfigureOpenIddict(); builder.ConfigureOpenIddict();
builder.ConfigureFeatureManagement(); builder.ConfigureFeatureManagement();
builder.ConfigureTenantManagement(); builder.ConfigureTenantManagement();
builder.ConfigureApplication();
} }
public DbSet<IdentityUser> Users { get; set; } public DbSet<IdentityUser> Users { get; set; }
@ -55,10 +57,4 @@ public class AdminDbContext :
public DbSet<Tenant> Tenants { get; set; } public DbSet<Tenant> Tenants { get; set; }
public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; } public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<ServiceCategory> ServiceCategories { get; set; }
public DbSet<HouseholdWorker> HouseholdWorkers { get; set; }
} }

View File

@ -1,6 +1,6 @@
using System.Diagnostics.CodeAnalysis; using KonSoft.Admin.Entities;
using KonSoft.Admin.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Diagnostics.CodeAnalysis;
using Volo.Abp; using Volo.Abp;
namespace KonSoft.Admin.EntityFrameworkCore.Configures; namespace KonSoft.Admin.EntityFrameworkCore.Configures;
@ -15,20 +15,23 @@ public static class ApplicationDbContextModelBuilderExtensions
{ {
b.ToTable(AdminConsts.DbTablePrefix + nameof(Order) + AdminConsts.DbSchema); b.ToTable(AdminConsts.DbTablePrefix + nameof(Order) + AdminConsts.DbSchema);
b.OwnsOne(o => o.Address, a => b.ComplexProperty(e => e.Address);
{
a.Property(p => p.ContactName).HasColumnName("ContactName").HasMaxLength(50);
a.Property(p => p.ContactPhone).HasColumnName("ContactPhone").HasMaxLength(20);
a.Property(p => p.DetailAddress).HasColumnName("DetailAddress").HasMaxLength(200);
a.Property(p => p.City).HasColumnName("City").HasMaxLength(50);
a.Property(p => p.District).HasColumnName("District").HasMaxLength(50);
});
}); });
builder.Entity<Product>(b =>
{
b.ToTable(AdminConsts.DbTablePrefix + nameof(Product) + AdminConsts.DbSchema);
});
builder.Entity<HouseholdWorker>(b =>
{
b.ToTable(AdminConsts.DbTablePrefix + nameof(HouseholdWorker) + AdminConsts.DbSchema);
});
builder.Entity<ServiceCategory>(b => builder.Entity<ServiceCategory>(b =>
{ {
b.ToTable(AdminConsts.DbTablePrefix + nameof(ServiceCategory) + AdminConsts.DbSchema); b.ToTable(AdminConsts.DbTablePrefix + nameof(ServiceCategory) + AdminConsts.DbSchema);
}); });
} }
} }

View File

@ -71,40 +71,38 @@ public abstract class PendingEfCoreMigrationsChecker<TDbContext> : ITransientDep
protected virtual async Task LockAndApplyDatabaseMigrationsAsync() 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}"); var dbContext = ServiceProvider.GetRequiredService<TDbContext>();
return;
}
using (CurrentTenant.Change(null)) var pendingMigrations = await dbContext
{ .Database
// Create database tables if needed .GetPendingMigrationsAsync();
using (var uow = UnitOfWorkManager.Begin(true, false))
if (pendingMigrations.Any())
{ {
var dbContext = ServiceProvider.GetRequiredService<TDbContext>(); await dbContext.Database.MigrateAsync();
var pendingMigrations = await dbContext
.Database
.GetPendingMigrationsAsync();
if (pendingMigrations.Any())
{
await dbContext.Database.MigrateAsync();
}
await uow.CompleteAsync();
} }
await ServiceProvider.GetRequiredService<IDataSeeder>() await uow.CompleteAsync();
.SeedAsync();
} }
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}...");
} }
} }