Просмотр исходного кода

😎调整初始化数据库时种子数据Id空值时自动添加Id

zuohuaijun 1 год назад
Родитель
Сommit
e853cd92e3
1 измененных файлов с 12 добавлено и 0 удалено
  1. 12 0
      Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

+ 12 - 0
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -372,6 +372,18 @@ public static class SqlSugarSetup
                 var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
                 Console.WriteLine($"添加数据 {entityInfo.DbTableName} ({config.ConfigId} - {++count}/{sum},数据量:{seedData.Count()})");
 
+                // 若实体包含Id字段,则设置为当前租户Id递增1
+                if (entityInfo.Columns.Any(u => u.PropertyName == nameof(EntityBaseId.Id)))
+                {
+                    var seedId = config.ConfigId.ToLong();
+                    foreach (var sd in seedData)
+                    {
+                        var id = sd.GetType().GetProperty(nameof(EntityBaseId.Id))!.GetValue(sd, null);
+                        if (id != null && (id.ToString() == "0" || string.IsNullOrWhiteSpace(id.ToString())))
+                            sd.GetType().GetProperty(nameof(EntityBaseId.Id))!.SetValue(sd, ++seedId);
+                    }
+                }
+
                 if (entityType.GetCustomAttribute<SplitTableAttribute>(true) != null)
                 {
                     //拆分表的操作需要实体类型,而通过反射很难实现