From 14c8f97e64c97f9b1e1d8099dbd05527e5f9648a Mon Sep 17 00:00:00 2001 From: FunCoder Date: Tue, 15 Apr 2025 15:47:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A4=E6=96=AD=E7=B3=BB=E7=BB=9F=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E6=98=AF=E7=AC=AC=E4=B8=80=E6=AC=A1=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=EF=BC=8C=E6=98=AF=E5=88=99=E5=BC=BA=E5=88=B6=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=95=B0=E6=8D=AE=E5=BA=93=E5=92=8C=E7=A7=8D=E5=AD=90?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs index 54f7b10f..f7027276 100644 --- a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs +++ b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs @@ -361,8 +361,11 @@ public static class SqlSugarSetup { 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}"); if (config.DbType != SqlSugar.DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase(); @@ -372,7 +375,7 @@ public static class SqlSugarSetup var (startups, oldVerion, currentVersion) = BeforeInitTable(dbProvider, config); // 初始化表结构 - if (config.TableSettings.EnableInitTable) + if (config.TableSettings.EnableInitTable || isfirst) { Log.Information($"初始化表结构 {config.DbType} - {config.ConfigId}"); 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);