升级.net10

This commit is contained in:
2026-04-29 15:56:41 +08:00
parent af1d4b3cd1
commit 8eaa6606c4
27 changed files with 256 additions and 711 deletions
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Query;
using System.Reflection;
namespace IRaCIS.Core.Infra.EFCore
@@ -6,37 +7,14 @@ namespace IRaCIS.Core.Infra.EFCore
public static class DynamicRelationalExtensions
{
//升级efcore9 RelationalQueryableExtensions-> EntityFrameworkQueryableExtensions
static MethodInfo UpdateMethodInfo =
typeof(RelationalQueryableExtensions).GetMethod(nameof(RelationalQueryableExtensions.ExecuteUpdate));
////升级efcore9 RelationalQueryableExtensions-> EntityFrameworkQueryableExtensions
//static MethodInfo UpdateMethodInfo =
// typeof(EntityFrameworkQueryableExtensions).GetMethod(nameof(EntityFrameworkQueryableExtensions.ExecuteUpdate));
static MethodInfo UpdateAsyncMethodInfo =
typeof(RelationalQueryableExtensions).GetMethod(nameof(RelationalQueryableExtensions.ExecuteUpdateAsync));
typeof(EntityFrameworkQueryableExtensions).GetMethod(nameof(EntityFrameworkQueryableExtensions.ExecuteUpdateAsync));
#region 使
public static int ExecuteUpdate(this IQueryable query, string fieldName, object? fieldValue)
{
var updateBody = BuildUpdateBody(query.ElementType,
new Dictionary<string, object?> { { fieldName, fieldValue } });
return (int)UpdateMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, new object?[] { query, updateBody });
}
public static int ExecuteUpdate(this IQueryable query, IReadOnlyDictionary<string, object?> fieldValues)
{
var updateBody = BuildUpdateBody(query.ElementType, fieldValues);
return (int)UpdateMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, new object?[] { query, updateBody });
}
public static Task<int> ExecuteUpdateAsync(this IQueryable query, string fieldName, object? fieldValue, CancellationToken cancellationToken = default)
{
var updateBody = BuildUpdateBody(query.ElementType,
new Dictionary<string, object?> { { fieldName, fieldValue } });
return (Task<int>)UpdateAsyncMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, new object?[] { query, updateBody, cancellationToken })!;
}
#endregion
public static Dictionary<string, object?> ExtractFieldValues<TSource>(this Expression<Func<TSource, TSource>> updateFactory)
{
@@ -64,50 +42,63 @@ namespace IRaCIS.Core.Infra.EFCore
public static Task<int> ExecuteUpdateAsync(this IQueryable query, IReadOnlyDictionary<string, object?> fieldValues, CancellationToken cancellationToken = default)
{
var updateBody = BuildUpdateBody(query.ElementType, fieldValues);
return (Task<int>)UpdateAsyncMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, new object?[] { query, updateBody, cancellationToken })!;
ParameterExpression propertyParam = Expression.Parameter(query.ElementType, "p");
Type SetPropertyBuilder = typeof(UpdateSettersBuilder<>).MakeGenericType(query.ElementType);
Action<object> BuilderAction = r => { };
foreach (KeyValuePair<string, object?> FieldValue in fieldValues)
{
MemberExpression propertyExp = Expression.PropertyOrField(propertyParam, FieldValue.Key);
MethodInfo? SetPropertyMethodInfo = (SetPropertyBuilder.GetMethods()
.FirstOrDefault(r => r.Name == nameof(UpdateSettersBuilder<>.SetProperty) && r.GetParameters().Any(p => p.ParameterType.BaseType == typeof(object)))?
.MakeGenericMethod(propertyExp.Type)) ?? throw new Exception("SetPropertyBuilder.SetProperty method is not found");
BuilderAction += r => SetPropertyMethodInfo.Invoke(r, [Expression.Lambda(propertyExp, propertyParam), FieldValue.Value]);
}
return (Task<int>)UpdateAsyncMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, [query, BuilderAction, cancellationToken])!;
}
static LambdaExpression BuildUpdateBody(Type entityType, IReadOnlyDictionary<string, object?> fieldValues)
{
var setParam = Expression.Parameter(typeof(SetPropertyCalls<>).MakeGenericType(entityType), "s");
var objParam = Expression.Parameter(entityType, "e");
Expression setBody = setParam;
//static LambdaExpression BuildUpdateBody(Type entityType, IReadOnlyDictionary<string, object?> fieldValues)
//{
// var setParam = Expression.Parameter(typeof(SetPropertyCalls<>).MakeGenericType(entityType), "s");
// var objParam = Expression.Parameter(entityType, "e");
foreach (var pair in fieldValues)
{
var propExpression = Expression.PropertyOrField(objParam, pair.Key);
var valueExpression = ValueForType(propExpression.Type, pair.Value);
// Expression setBody = setParam;
// s.SetProperty(e => e.SomeField, value)
setBody = Expression.Call(setBody, nameof(SetPropertyCalls<object>.SetProperty),
new[] { propExpression.Type }, Expression.Lambda(propExpression, objParam), valueExpression);
// foreach (var pair in fieldValues)
// {
// var propExpression = Expression.PropertyOrField(objParam, pair.Key);
// var valueExpression = ValueForType(propExpression.Type, pair.Value);
}
// // s.SetProperty(e => e.SomeField, value)
// setBody = Expression.Call(setBody, nameof(SetPropertyCalls<object>.SetProperty),
// new[] { propExpression.Type }, Expression.Lambda(propExpression, objParam), valueExpression);
// s => s.SetProperty(e => e.SomeField, value)
var updateBody = Expression.Lambda(setBody, setParam);
// }
return updateBody;
}
// // s => s.SetProperty(e => e.SomeField, value)
// var updateBody = Expression.Lambda(setBody, setParam);
static Expression ValueForType(Type desiredType, object? value)
{
if (value == null)
{
return Expression.Default(desiredType);
}
// return updateBody;
//}
if (value.GetType() != desiredType)
{
return Expression.Convert(Expression.Constant(value), desiredType);
}
//static Expression ValueForType(Type desiredType, object? value)
//{
// if (value == null)
// {
// return Expression.Default(desiredType);
// }
return Expression.Constant(value);
}
// if (value.GetType() != desiredType)
// {
// return Expression.Convert(Expression.Constant(value), desiredType);
// }
// return Expression.Constant(value);
//}
}
}