InsertNav Ilist

This commit is contained in:
sunkaixuan 2025-04-02 11:26:35 +08:00
parent a45f728062
commit 6299757220

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
@ -40,6 +41,10 @@ namespace SqlSugar
} }
children.AddRange(childs); children.AddRange(childs);
} }
else if (childs == null && parentNavigateProperty.PropertyInfo.GetValue(item) is IList ilist && ilist != null&& ilist.Count>0)
{
childs = GetIChildsBylList(children, thisFkColumn, parentValue, ilist);
}
} }
var isTreeChild = GetIsTreeChild(parentEntity, thisEntity); var isTreeChild = GetIsTreeChild(parentEntity, thisEntity);
Check.ExceptionEasy(thisPkColumn == null, $"{thisEntity.EntityName}need primary key", $"实体{thisEntity.EntityName}需要主键"); Check.ExceptionEasy(thisPkColumn == null, $"{thisEntity.EntityName}need primary key", $"实体{thisEntity.EntityName}需要主键");
@ -54,6 +59,17 @@ namespace SqlSugar
SetNewParent<TChild>(thisEntity, thisPkColumn); SetNewParent<TChild>(thisEntity, thisPkColumn);
} }
private static List<TChild> GetIChildsBylList<TChild>(List<TChild> children, EntityColumnInfo thisFkColumn, object parentValue, IList ilist) where TChild : class, new()
{
List<TChild> childs = ilist.Cast<TChild>().ToList();
foreach (var child in childs)
{
thisFkColumn.PropertyInfo.SetValue(child, parentValue, null);
}
children.AddRange(childs);
return childs;
}
private bool GetIsTreeChild(EntityInfo parentEntity , EntityInfo thisEntity) private bool GetIsTreeChild(EntityInfo parentEntity , EntityInfo thisEntity)
{ {
return this.NavContext?.Items?.Any() == true && parentEntity.Type == thisEntity.Type; return this.NavContext?.Items?.Any() == true && parentEntity.Type == thisEntity.Type;