From 62997572201b3f30e925710b92c4de2eb1c8de6c Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 2 Apr 2025 11:26:35 +0800 Subject: [PATCH] InsertNav Ilist --- .../InsertNavProviderOneToMany.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToMany.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToMany.cs index d0ee266f1..6911ef1ac 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToMany.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToMany.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -40,6 +41,10 @@ namespace SqlSugar } 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); Check.ExceptionEasy(thisPkColumn == null, $"{thisEntity.EntityName}need primary key", $"实体{thisEntity.EntityName}需要主键"); @@ -54,6 +59,17 @@ namespace SqlSugar 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) { return this.NavContext?.Items?.Any() == true && parentEntity.Type == thisEntity.Type;