diff --git a/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Persistence/AppDataSeeder.cs b/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Persistence/AppDataSeeder.cs index 079a573..d679268 100644 --- a/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Persistence/AppDataSeeder.cs +++ b/src/Infrastructure/TakeoutSaaS.Infrastructure/App/Persistence/AppDataSeeder.cs @@ -30,17 +30,23 @@ public sealed class AppDataSeeder( /// public async Task StartAsync(CancellationToken cancellationToken) { - if (!_options.Enabled) - { - logger.LogInformation("AppSeed 未启用,跳过业务数据初始化"); - return; - } - using var scope = serviceProvider.CreateScope(); var appDbContext = scope.ServiceProvider.GetRequiredService(); var dictionaryDbContext = scope.ServiceProvider.GetRequiredService(); + + // 1. 启动时优先确保 App/Dictionary 数据库迁移到最新版本。 + await appDbContext.Database.MigrateAsync(cancellationToken); + await dictionaryDbContext.Database.MigrateAsync(cancellationToken); + + if (!_options.Enabled) + { + logger.LogInformation("AppSeed 未启用,仅执行数据库迁移,跳过业务数据初始化"); + return; + } + var tenantContextAccessor = scope.ServiceProvider.GetRequiredService(); + // 2. 执行业务数据种子。 await EnsureSystemTenantAsync(appDbContext, cancellationToken); var defaultTenantId = await EnsureDefaultTenantAsync(appDbContext, cancellationToken); await EnsureDictionarySeedsAsync(dictionaryDbContext, tenantContextAccessor, defaultTenantId, cancellationToken);