Merge pull request '判断系统是否是第一次启动,是则强制初始化数据库和种子数据' (#312) from Junong/Admin.NET.Pro:v2 into v2

Reviewed-on: https://code.adminnet.top/Admin.NET/Admin.NET.Pro/pulls/312
This commit is contained in:
zuohuaijun 2025-04-15 17:21:11 +08:00
commit e1f46f6a66

View File

@ -361,8 +361,11 @@ public static class SqlSugarSetup
{ {
SqlSugarScopeProvider dbProvider = db.GetConnectionScope(config.ConfigId); SqlSugarScopeProvider dbProvider = db.GetConnectionScope(config.ConfigId);
// 判断系统是否是第一次启动,是则强制初始化数据库和种子数据
bool isfirst = dbProvider.CurrentConnectionConfig.ConfigId.ToString() == SqlSugarConst.MainConfigId && !dbProvider.DbMaintenance.IsAnyTable(nameof(SysConfig));
// 初始化/创建数据库 // 初始化/创建数据库
if (config.DbSettings.EnableInitDb) if (config.DbSettings.EnableInitDb || isfirst)
{ {
Log.Information($"初始化数据库 {config.DbType} - {config.ConfigId} - {config.ConnectionString}"); Log.Information($"初始化数据库 {config.DbType} - {config.ConfigId} - {config.ConnectionString}");
if (config.DbType != SqlSugar.DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase(); if (config.DbType != SqlSugar.DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase();
@ -372,7 +375,7 @@ public static class SqlSugarSetup
var (startups, oldVerion, currentVersion) = BeforeInitTable(dbProvider, config); var (startups, oldVerion, currentVersion) = BeforeInitTable(dbProvider, config);
// 初始化表结构 // 初始化表结构
if (config.TableSettings.EnableInitTable) if (config.TableSettings.EnableInitTable || isfirst)
{ {
Log.Information($"初始化表结构 {config.DbType} - {config.ConfigId}"); Log.Information($"初始化表结构 {config.DbType} - {config.ConfigId}");
var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false)) var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))
@ -410,7 +413,7 @@ public static class SqlSugarSetup
} }
// 初始化种子数据 // 初始化种子数据
if (config.SeedSettings.EnableInitSeed) InitSeedData(db, config); if (config.SeedSettings.EnableInitSeed || isfirst) InitSeedData(db, config);
// 初始化种子数据之后——系统版本号 // 初始化种子数据之后——系统版本号
AfterInitSeed(dbProvider, config, startups, oldVerion, currentVersion); AfterInitSeed(dbProvider, config, startups, oldVerion, currentVersion);