chore 重构数据库迁移逻辑并优化实体配置
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
using KonSoft.Admin.DbMigrations;
|
||||
using KonSoft.Admin.EntityFrameworkCore;
|
||||
using KonSoft.Shared.Hosting.AspNetCore;
|
||||
using KonSoft.Shared.Hosting.Microservices;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.Threading.Tasks;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.BackgroundJobs;
|
||||
using Volo.Abp.Modularity;
|
||||
@ -74,4 +76,10 @@ public class AdminHttpApiHostModule : AbpModule
|
||||
app.UseAbpSerilogEnrichers();
|
||||
app.UseConfiguredEndpoints();
|
||||
}
|
||||
|
||||
public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
|
||||
{
|
||||
await context.ServiceProvider.GetRequiredService<AdminPendingEfCoreMigrationsChecker>()
|
||||
.CheckAndApplyDatabaseMigrationsAsync();
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
using AutoMapper;
|
||||
using KonSoft.Admin.Dtos;
|
||||
using KonSoft.Admin.Entities;
|
||||
using KonSoft.Admin.ValueObjects;
|
||||
|
||||
namespace KonSoft.Admin;
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@ using KonSoft.Admin.Dtos;
|
||||
using KonSoft.Admin.Entities;
|
||||
using KonSoft.Admin.IApplicationServices;
|
||||
using KonSoft.Admin.IRepositories;
|
||||
using KonSoft.Admin.ValueObjects;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Volo.Abp.Application.Services;
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using KonSoft.Admin.Enums;
|
||||
using KonSoft.Admin.ValueObjects;
|
||||
using Volo.Abp.Domain.Entities.Auditing;
|
||||
|
||||
namespace KonSoft.Admin.Entities;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using Volo.Abp.Domain.Values;
|
||||
|
||||
namespace KonSoft.Admin.Entities;
|
||||
namespace KonSoft.Admin.ValueObjects;
|
||||
|
||||
/// <summary>
|
||||
/// 地址
|
||||
@ -1,4 +1,4 @@
|
||||
using KonSoft.Admin.Entities;
|
||||
using KonSoft.Admin.EntityFrameworkCore.Configures;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Volo.Abp.AuditLogging.EntityFrameworkCore;
|
||||
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
|
||||
@ -41,6 +41,8 @@ public class AdminDbContext :
|
||||
builder.ConfigureOpenIddict();
|
||||
builder.ConfigureFeatureManagement();
|
||||
builder.ConfigureTenantManagement();
|
||||
|
||||
builder.ConfigureApplication();
|
||||
}
|
||||
|
||||
public DbSet<IdentityUser> Users { get; set; }
|
||||
@ -55,10 +57,4 @@ public class AdminDbContext :
|
||||
|
||||
public DbSet<Tenant> Tenants { 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; }
|
||||
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using KonSoft.Admin.Entities;
|
||||
using KonSoft.Admin.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Volo.Abp;
|
||||
|
||||
namespace KonSoft.Admin.EntityFrameworkCore.Configures;
|
||||
@ -15,20 +15,23 @@ public static class ApplicationDbContextModelBuilderExtensions
|
||||
{
|
||||
b.ToTable(AdminConsts.DbTablePrefix + nameof(Order) + AdminConsts.DbSchema);
|
||||
|
||||
b.OwnsOne(o => o.Address, a =>
|
||||
{
|
||||
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);
|
||||
});
|
||||
b.ComplexProperty(e => e.Address);
|
||||
});
|
||||
|
||||
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 =>
|
||||
{
|
||||
b.ToTable(AdminConsts.DbTablePrefix + nameof(ServiceCategory) + AdminConsts.DbSchema);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@ -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}...");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user