Jelajahi Sumber

😎1、控制台显示正在初始化的表和种子数据 2、其他优化

zuohuaijun 1 tahun lalu
induk
melakukan
4430bbc13d

+ 1 - 1
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", // 默认库标识-禁止修改

+ 1 - 1
Admin.NET/Admin.NET.Core/Entity/SysMenu.cs

@@ -74,7 +74,7 @@ public partial class SysMenu : EntityBase
     /// </summary>
     [SugarColumn(ColumnDescription = "图标", Length = 128)]
     [MaxLength(128)]
-    public string? Icon { get; set; }
+    public string? Icon { get; set; } = "ele-Menu";
 
     /// <summary>
     /// 是否内嵌

+ 28 - 9
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -289,6 +289,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();
         }
@@ -296,6 +297,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<IgnoreTableAttribute>().Any())
                 .WhereIF(config.TableSettings.EnableIncreTable, u => u.IsDefined(typeof(IncreTableAttribute), false)).ToList();
@@ -307,8 +309,10 @@ public static class SqlSugarSetup
             else
                 entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库
 
+            int count = 0, sum = entityTypes.Count;
             foreach (var entityType in entityTypes)
             {
+                Console.WriteLine($"创建表 {entityType} ({config.ConfigId} - {++count}/{sum})");
                 if (entityType.GetCustomAttribute<SplitTableAttribute>() == null)
                     dbProvider.CodeFirst.InitTables(entityType);
                 else
@@ -319,10 +323,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();
@@ -348,19 +354,32 @@ public static class SqlSugarSetup
                 if (seedData == null) continue;
 
                 var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
-                if (entityInfo.Columns.Any(u => u.IsPrimarykey))
+                Console.WriteLine($"添加数据 {entityInfo.DbTableName} ({config.ConfigId} - {++count}/{sum})");
+
+                if (entityType.GetCustomAttribute<SplitTableAttribute>(true) != null)
                 {
-                    // 按主键进行批量增加和更新
-                    var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage();
-                    storage.AsInsertable.ExecuteCommand();
-                    if (seedType.GetCustomAttribute<IgnoreUpdateSeedAttribute>() == null) // 有忽略更新种子特性时则不更新
-                        storage.AsUpdateable.IgnoreColumns(entityInfo.Columns.Where(c => c.PropertyInfo.GetCustomAttribute<IgnoreUpdateSeedColumnAttribute>() != null).Select(c => c.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<IgnoreUpdateSeedAttribute>() == null) // 有忽略更新种子特性时则不更新
+                            storage.AsUpdateable.IgnoreColumns(entityInfo.Columns.Where(u => u.PropertyInfo.GetCustomAttribute<IgnoreUpdateSeedColumnAttribute>() != null).Select(u => u.PropertyName).ToArray()).ExecuteCommand();
+                    }
+                    else
+                    {
+                        // 无主键则只进行插入
+                        if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
+                            dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand();
+                    }
                 }
             }
         }

+ 2 - 2
Web/src/views/system/user/component/editUser.vue

@@ -8,7 +8,7 @@
 				</div>
 			</template>
 			<el-tabs v-loading="state.loading" v-model="state.selectedTabName">
-				<el-tab-pane label="基础信息" style="height: 550px">
+				<el-tab-pane label="基础信息" style="height: 550px; overflow-y: auto; overflow-x: hidden">
 					<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto">
 						<el-row :gutter="35">
 							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
@@ -144,7 +144,7 @@
 						</el-row>
 					</el-form>
 				</el-tab-pane>
-				<el-tab-pane label="档案信息" style="height: 550px">
+				<el-tab-pane label="档案信息" style="height: 550px; overflow-y: auto; overflow-x: hidden">
 					<el-form :model="state.ruleForm" label-width="auto">
 						<el-row :gutter="35">
 							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">