From d82c23764ab243c2103341a45b1f933c644977e0 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 11 Sep 2023 00:30:30 +0800 Subject: [PATCH] UpdateNav.IncludeByNameString --- .../ExecuteNavProvider/UpdateNavTask.cs | 16 ++++++ .../UpdateProvider/UpdateNavMethodInfo.cs | 52 +++++++++++++++++++ .../SqlSugar/Utilities/UtilMethods.cs | 12 ++++- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateNavMethodInfo.cs diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavTask.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavTask.cs index 3c100eb7a..1a97e5043 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavTask.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavTask.cs @@ -59,6 +59,22 @@ namespace SqlSugar result.NavContext = UpdateNavProvider.NavContext; return result; } + + public UpdateNavMethodInfo IncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions=null) + { + UpdateNavMethodInfo result = new UpdateNavMethodInfo(); + result.Context = UpdateNavProvider._Context; + var entityInfo = result.Context.EntityMaintenance.GetEntityInfo(); + Type properyItemType; + Expression exp =UtilMethods.GetIncludeExpression(navMemberName, entityInfo, out properyItemType); + var method = this.GetType().GetMyMethod("Include", 2) + .MakeGenericMethod(properyItemType); + var obj = method.Invoke(this, new object[] { exp, updateNavOptions }); + result.MethodInfos = obj; + return result; + } + + } public class UpdateNavTask where T : class, new() where Root : class, new() { diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateNavMethodInfo.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateNavMethodInfo.cs new file mode 100644 index 000000000..096a6dbce --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateNavMethodInfo.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class UpdateNavMethodInfo + { + internal object MethodInfos { get; set; } + internal SqlSugarProvider Context { get; set; } + + public UpdateNavMethodInfo IncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions = null) + { + var type = MethodInfos.GetType().GetGenericArguments()[0]; + var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type); + Type properyItemType; + Expression exp = UtilMethods.GetIncludeExpression(navMemberName, entityInfo, out properyItemType); + var method = this.GetType().GetMyMethod("Include", 2) + .MakeGenericMethod(properyItemType); + var obj = method.Invoke(this, new object[] { exp, updateNavOptions }); + this.MethodInfos = obj; + return this; + } + public UpdateNavMethodInfo ThenIncludeByNameString(string IncludeByNameString, UpdateNavOptions updateNavOptions = null) + { + var type = MethodInfos.GetType().GetGenericArguments()[0]; + var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type); + Type properyItemType; + Expression exp = UtilMethods.GetIncludeExpression(navMemberName, entityInfo, out properyItemType); + var method = this.GetType().GetMyMethod("ThenInclude", 2) + .MakeGenericMethod(properyItemType); + var obj = method.Invoke(this, new object[] { exp, updateNavOptions }); + this.MethodInfos = obj; + return this; + } + public async Task ExecuteCommandAsync() + { + if (Context == null) return false; + var result = MethodInfos.GetType().GetMethod("ExecuteCommandAsync").Invoke(MethodInfos, new object[] { }); + return await (Task)result; + } + public bool ExecuteCommand() + { + if (Context == null) return false; + var result = MethodInfos.GetType().GetMethod("ExecuteCommand").Invoke(MethodInfos, new object[] { }); + return (bool)result; + } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs index 6c8fcb5b1..bcfe06427 100644 --- a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs @@ -18,7 +18,17 @@ namespace SqlSugar { public class UtilMethods { - + internal static Expression GetIncludeExpression(string navMemberName, EntityInfo entityInfo, out Type properyItemType) + { + var navInfo = entityInfo.Columns.Where(it => it.Navigat != null && it.PropertyName.EqualCase(navMemberName)).FirstOrDefault(); + var properyType = navInfo.PropertyInfo.PropertyType; + properyItemType = properyType; + if (properyType.FullName.IsCollectionsList()) + { + properyItemType = properyType.GetGenericArguments()[0]; + } + return ExpressionBuilderHelper.CreateExpressionSelectField(entityInfo.Type, navInfo.PropertyName, properyType); + } public static string RemoveEqualOne(string value) { value = value.TrimEnd(' ').TrimEnd('1').TrimEnd('=');