Quellcode durchsuchen

😂修复无主键的种子数据初始化问题

zuohuaijun vor 3 Jahren
Ursprung
Commit
da73dd45cb

+ 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" />
+    <PackageReference Include="SqlSugarCore" Version="5.1.3.42-preview07" />
     <PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.24" />
     <PackageReference Include="UAParser" Version="3.1.47" />
     <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />

+ 15 - 4
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -228,10 +228,21 @@ public static class SqlSugarSetup
             if (tAtt != null && tAtt.configId.ToString() != config.ConfigId) continue;
             if (tAtt == null && config.ConfigId != SqlSugarConst.ConfigId) continue;
 
-            var storage = dbProvider.CopyNew().StorageableByObject(seedData.ToList()).ToStorage();
-            storage.AsInsertable.ExecuteCommand();
-            var ignoreUpdate = hasDataMethod.GetCustomAttribute<IgnoreUpdateAttribute>();
-            if (ignoreUpdate == null) storage.AsUpdateable.ExecuteCommand();
+            var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
+            if (entityInfo.Columns.Any(u => u.IsPrimarykey))
+            {
+                // 按主键进行批量增加和更新
+                var storage = dbProvider.CopyNew().StorageableByObject(seedData.ToList()).ToStorage();
+                storage.AsInsertable.ExecuteCommand();
+                var ignoreUpdate = hasDataMethod.GetCustomAttribute<IgnoreUpdateAttribute>();
+                if (ignoreUpdate == null) storage.AsUpdateable.ExecuteCommand();
+            }
+            else
+            {
+                // 无主键则只进行插入
+                if (dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Count() <= 0)
+                    dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand();
+            }
         }
     }