From 8361c21532d2871c04fbbf9db5ce77459d4056f2 Mon Sep 17 00:00:00 2001 From: coolcalf <28551@qq.com> Date: Sun, 21 Jul 2024 16:32:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=8F=B0=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E6=AD=A3=E5=9C=A8=E5=88=9D=E5=A7=8B=E5=8C=96=E7=9A=84=E8=A1=A8?= =?UTF-8?q?=E5=92=8C=E7=A7=8D=E5=AD=90=E6=95=B0=E6=8D=AE=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=8B=86=E5=88=86=E8=A1=A8=E6=95=B0=E6=8D=AE=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=20=E8=8F=9C=E5=8D=95icon=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Configuration/Database.json | 2 +- Admin.NET/Admin.NET.Core/Entity/SysMenu.cs | 2 +- .../Admin.NET.Core/SqlSugar/SqlSugarSetup.cs | 41 ++++++++++++++----- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Admin.NET/Admin.NET.Application/Configuration/Database.json b/Admin.NET/Admin.NET.Application/Configuration/Database.json index 5d36e072..4802749e 100644 --- a/Admin.NET/Admin.NET.Application/Configuration/Database.json +++ b/Admin.NET/Admin.NET.Application/Configuration/Database.json @@ -4,7 +4,7 @@ // 详细数据库配置见SqlSugar官网(第一个为默认库),极力推荐 PostgreSQL 数据库 // 数据库连接字符串参考地址:https://www.connectionstrings.com/ "DbConnection": { - "EnableConsoleSql": true, // 启用控制台打印SQL + "EnableConsoleSql": false, // 启用控制台打印SQL "ConnectionConfigs": [ { //"ConfigId": "1300000000001", // 默认库标识-禁止修改 diff --git a/Admin.NET/Admin.NET.Core/Entity/SysMenu.cs b/Admin.NET/Admin.NET.Core/Entity/SysMenu.cs index 3f6a51a9..c7e08dbf 100644 --- a/Admin.NET/Admin.NET.Core/Entity/SysMenu.cs +++ b/Admin.NET/Admin.NET.Core/Entity/SysMenu.cs @@ -74,7 +74,7 @@ public partial class SysMenu : EntityBase /// [SugarColumn(ColumnDescription = "图标", Length = 128)] [MaxLength(128)] - public string? Icon { get; set; } + public string? Icon { get; set; } = "ele-Setting"; /// /// 是否内嵌 diff --git a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs index 7d684e28..aa75d7a5 100644 --- a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs +++ b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs @@ -284,6 +284,7 @@ public static class SqlSugarSetup // 初始化/创建数据库 if (config.DbSettings.EnableInitDb) { + Log.Information($"初始化 {config.DbType} 数据库 {config.ConfigId} {config.ConnectionString}"); if (config.DbType != SqlSugar.DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase(); } @@ -291,6 +292,7 @@ public static class SqlSugarSetup // 初始化表结构 if (config.TableSettings.EnableInitTable) { + Log.Information($"初始化 {config.DbType} 表结构 {config.ConfigId}"); var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false)) .Where(u => !u.GetCustomAttributes().Any()) .WhereIF(config.TableSettings.EnableIncreTable, u => u.IsDefined(typeof(IncreTableAttribute), false)).ToList(); @@ -301,9 +303,11 @@ public static class SqlSugarSetup entityTypes = entityTypes.Where(u => u.GetCustomAttributes().Any()).ToList(); else entityTypes = entityTypes.Where(u => u.GetCustomAttribute()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库 - + + int count = 0, sum = entityTypes.Count(); foreach (var entityType in entityTypes) { + Console.WriteLine($"创建表({config.ConfigId} - {++count}/{sum}):{entityType}"); if (entityType.GetCustomAttribute() == null) dbProvider.CodeFirst.InitTables(entityType); else @@ -314,10 +318,12 @@ public static class SqlSugarSetup // 初始化种子数据 if (config.SeedSettings.EnableInitSeed) { + Log.Information($"初始化 {config.DbType} 种子数据 {config.ConfigId}"); var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>)))) .WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false)) .OrderBy(u => u.GetCustomAttributes(typeof(SeedDataAttribute), false).Length > 0 ? ((SeedDataAttribute)u.GetCustomAttributes(typeof(SeedDataAttribute), false)[0]).Order : 0).ToList(); - + + int count = 0, sum = seedDataTypes.Count(); foreach (var seedType in seedDataTypes) { var entityType = seedType.GetInterfaces().First().GetGenericArguments().First(); @@ -343,19 +349,32 @@ public static class SqlSugarSetup if (seedData == null) continue; var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType); - if (entityInfo.Columns.Any(u => u.IsPrimarykey)) + Console.WriteLine($"添加数据({config.ConfigId} - {++count}/{sum}):{entityInfo.DbTableName}"); + + if (entityType.GetCustomAttribute(true) != null) { - // 按主键进行批量增加和更新 - var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage(); - storage.AsInsertable.ExecuteCommand(); - if (seedType.GetCustomAttribute() == null) // 有忽略更新种子特性时则不更新 - storage.AsUpdateable.IgnoreColumns(entityInfo.Columns.Where(u => u.PropertyInfo.GetCustomAttribute() != null).Select(u => u.PropertyName).ToArray()).ExecuteCommand(); + //拆分表的操作需要实体类型,而通过反射很难实现 + //所以,这里将Init方法写在“种子数据类”内部,再传入 db 反射调用 + var hasInitMethod = seedType.GetMethod("Init"); + var parameters = new object[] { db }; + hasInitMethod?.Invoke(instance, parameters); } else { - // 无主键则只进行插入 - if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any()) - dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand(); + if (entityInfo.Columns.Any(u => u.IsPrimarykey)) + { + // 按主键进行批量增加和更新 + var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage(); + storage.AsInsertable.ExecuteCommand(); + if (seedType.GetCustomAttribute() == null) // 有忽略更新种子特性时则不更新 + storage.AsUpdateable.IgnoreColumns(entityInfo.Columns.Where(u => u.PropertyInfo.GetCustomAttribute() != null).Select(u => u.PropertyName).ToArray()).ExecuteCommand(); + } + else + { + // 无主键则只进行插入 + if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any()) + dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand(); + } } } }