Quellcode durchsuchen

简化种子数据初始化

zuohuaijun vor 3 Jahren
Ursprung
Commit
2a319970e6

+ 1 - 1
Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj

@@ -30,7 +30,7 @@
     <PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="2.21.1" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="2.15.2" />
-    <PackageReference Include="SqlSugarCore" Version="5.1.3.41-preview07" />
+    <PackageReference Include="SqlSugarCore" Version="5.1.3.41-preview08" />
     <PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.23" />
     <PackageReference Include="UAParser" Version="3.1.47" />
     <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />

+ 5 - 20
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -189,7 +189,7 @@ public static class SqlSugarSetup
     {
         if (!config.EnableInitDb) return;
 
-        var dbProvider = db.GetConnectionScope(config.ConfigId);
+        SqlSugarScopeProvider dbProvider = db.GetConnectionScope(config.ConfigId);
 
         // 创建数据库
         if (config.DbType != SqlSugar.DbType.Oracle)
@@ -229,25 +229,10 @@ public static class SqlSugarSetup
             if (tAtt != null && tAtt.configId.ToString() != config.ConfigId) continue;
             if (tAtt == null && config.ConfigId != SqlSugarConst.ConfigId) continue;
 
-            var seedDataTable = seedData.ToList().ToDataTable();
-            seedDataTable.TableName = dbProvider.EntityMaintenance.GetEntityInfo(entityType).DbTableName;
-            if (config.EnableUnderLine) // 驼峰转下划线
-            {
-                foreach (DataColumn col in seedDataTable.Columns)
-                    col.ColumnName = UtilMethods.ToUnderLine(col.ColumnName);
-            }
-            if (seedDataTable.Columns.Contains(SqlSugarConst.PrimaryKey))
-            {
-                var storage = dbProvider.CopyNew().Storageable(seedDataTable).WhereColumns(SqlSugarConst.PrimaryKey).ToStorage();
-                storage.AsInsertable.ExecuteCommand();
-                var ignoreUpdate = hasDataMethod.GetCustomAttribute<IgnoreUpdateAttribute>();
-                if (ignoreUpdate == null) storage.AsUpdateable.ExecuteCommand();
-            }
-            else // 没有主键或者不是预定义的主键(有重复的可能)
-            {
-                var storage = dbProvider.CopyNew().Storageable(seedDataTable).ToStorage();
-                storage.AsInsertable.ExecuteCommand();
-            }
+            var storage = dbProvider.CopyNew().StorageableByObject(seedData.ToList()).ToStorage();
+            storage.AsInsertable.ExecuteCommand();
+            var ignoreUpdate = hasDataMethod.GetCustomAttribute<IgnoreUpdateAttribute>();
+            if (ignoreUpdate == null) storage.AsUpdateable.ExecuteCommand();
         }
     }