Merge pull request '调整初始化数据库时种子数据Id空值时自动添加Id' (#140) from ko88/Admin.NET.Pro:main into main

Reviewed-on: http://101.43.53.74:3000/Admin.NET/Admin.NET.Pro/pulls/140
This commit is contained in:
zuohuaijun 2024-09-20 00:13:54 +08:00
commit 27416977f1

View File

@ -363,6 +363,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)
{
// 拆分表的操作需要实体类型,而通过反射很难实现