UpdateNav.IncludeByNameString

This commit is contained in:
sunkaixuan
2023-09-11 00:30:30 +08:00
parent 06be7a786b
commit d82c23764a
3 changed files with 79 additions and 1 deletions

View File

@@ -59,6 +59,22 @@ namespace SqlSugar
result.NavContext = UpdateNavProvider.NavContext; result.NavContext = UpdateNavProvider.NavContext;
return result; return result;
} }
public UpdateNavMethodInfo IncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions=null)
{
UpdateNavMethodInfo result = new UpdateNavMethodInfo();
result.Context = UpdateNavProvider._Context;
var entityInfo = result.Context.EntityMaintenance.GetEntityInfo<T>();
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<Root, T> where T : class, new() where Root : class, new() public class UpdateNavTask<Root, T> where T : class, new() where Root : class, new()
{ {

View File

@@ -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<bool> ExecuteCommandAsync()
{
if (Context == null) return false;
var result = MethodInfos.GetType().GetMethod("ExecuteCommandAsync").Invoke(MethodInfos, new object[] { });
return await (Task<bool>)result;
}
public bool ExecuteCommand()
{
if (Context == null) return false;
var result = MethodInfos.GetType().GetMethod("ExecuteCommand").Invoke(MethodInfos, new object[] { });
return (bool)result;
}
}
}

View File

@@ -18,7 +18,17 @@ namespace SqlSugar
{ {
public class UtilMethods 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) public static string RemoveEqualOne(string value)
{ {
value = value.TrimEnd(' ').TrimEnd('1').TrimEnd('='); value = value.TrimEnd(' ').TrimEnd('1').TrimEnd('=');