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(); + } } } }