@@ -20,6 +20,8 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||
static MethodInfo UpdateAsyncMethodInfo =
|
||||
typeof(RelationalQueryableExtensions).GetMethod(nameof(RelationalQueryableExtensions.ExecuteUpdateAsync));
|
||||
|
||||
#region 避免使用
|
||||
|
||||
public static int ExecuteUpdate(this IQueryable query, string fieldName, object? fieldValue)
|
||||
{
|
||||
var updateBody = BuildUpdateBody(query.ElementType,
|
||||
@@ -28,15 +30,53 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||
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)
|
||||
{
|
||||
var dic = new Dictionary<string, object?>();
|
||||
var obj = (TSource)Activator.CreateInstance(typeof(TSource));
|
||||
|
||||
Func<TSource, TSource> func = updateFactory.Compile();
|
||||
|
||||
TSource applyObj = func(obj);
|
||||
|
||||
var propList = ((MemberInitExpression)updateFactory.Body).Bindings.Select(mb => mb.Member.Name)
|
||||
.Select(propName => typeof(TSource).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)).ToList();
|
||||
|
||||
|
||||
foreach (PropertyInfo prop in propList)
|
||||
{
|
||||
object value = prop.GetValue(applyObj);
|
||||
dic.Add(prop.Name, value);
|
||||
}
|
||||
|
||||
return dic;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 })!;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static LambdaExpression BuildUpdateBody(Type entityType, IReadOnlyDictionary<string, object?> fieldValues)
|
||||
{
|
||||
var setParam = Expression.Parameter(typeof(SetPropertyCalls<>).MakeGenericType(entityType), "s");
|
||||
|
||||
@@ -5,6 +5,7 @@ using IRaCIS.Core.Infrastructure;
|
||||
using IRaCIS.Core.Infrastructure.Extention;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using SharpCompress.Factories;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -170,7 +171,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||
if (where == null) throw new ArgumentNullException(nameof(where));
|
||||
|
||||
|
||||
#region batch delete bug
|
||||
#region history 使用扩展删除包,同时自动赋值更新人 更新时间
|
||||
|
||||
var bindings = ((MemberInitExpression)updateFactory.Body).Bindings.ToList();
|
||||
|
||||
@@ -205,62 +206,36 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||
#endregion
|
||||
|
||||
|
||||
#region efcore 7
|
||||
|
||||
//Dictionary<string, object> propValues = new Dictionary<string, object>();
|
||||
|
||||
//// 获取表达式体的 MemberInitExpression
|
||||
//var memberInitExpr = (MemberInitExpression)updateFactory.Body;
|
||||
|
||||
|
||||
|
||||
//// 获取初始化的绑定信息
|
||||
//var bindings = memberInitExpr.Bindings.ToList();
|
||||
|
||||
//var hasPropNameList = bindings.Select(t => t.Member.Name).ToList();
|
||||
|
||||
|
||||
//// 遍历绑定信息
|
||||
//foreach (MemberAssignment binding in bindings)
|
||||
#region efcore 7 & 8
|
||||
//{
|
||||
// // 获取属性名
|
||||
// string propName = binding.Member.Name;
|
||||
// var fieldValues = updateFactory.ExtractFieldValues();
|
||||
|
||||
// // 获取属性值,需要对表达式进行编译和调用
|
||||
// var propValue = Expression.Lambda(binding.Expression).Compile().DynamicInvoke();
|
||||
// var hasPropNameList = ((MemberInitExpression)updateFactory.Body).Bindings.Select(t => t.Member.Name).ToList();
|
||||
|
||||
// // 现在,你可以访问属性名和属性值
|
||||
// propValues.Add(propName, propValue);
|
||||
//}
|
||||
|
||||
//if (typeof(IAuditUpdate).IsAssignableFrom(typeof(T)))
|
||||
//{
|
||||
|
||||
// if (!hasPropNameList.Contains(nameof(IAuditUpdate.UpdateTime)))
|
||||
// if (typeof(IAuditUpdate).IsAssignableFrom(typeof(T)))
|
||||
// {
|
||||
// propValues.Add(nameof(IAuditUpdate.UpdateTime), DateTime.Now);
|
||||
|
||||
// if (!hasPropNameList.Contains(nameof(IAuditUpdate.UpdateTime)))
|
||||
// {
|
||||
// fieldValues.Add(nameof(IAuditUpdate.UpdateTime), DateTime.Now);
|
||||
// }
|
||||
|
||||
// if (!hasPropNameList.Contains(nameof(IAuditUpdate.UpdateUserId)))
|
||||
// {
|
||||
// fieldValues.Add(nameof(IAuditUpdate.UpdateUserId), updateUserId);
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (!hasPropNameList.Contains(nameof(IAuditUpdate.UpdateUserId)))
|
||||
// {
|
||||
// propValues.Add(nameof(IAuditUpdate.UpdateUserId), updateUserId);
|
||||
|
||||
// }
|
||||
|
||||
// return await _dbContext.Set<T>().IgnoreQueryFilters().Where(where).ExecuteUpdateAsync(fieldValues).ConfigureAwait(false) > 0;
|
||||
//}
|
||||
|
||||
//return await _dbContext.Set<T>().IgnoreQueryFilters().Where(where).ExecuteUpdateAsync(s => s.SetProperty(t => "UpdateTime", u => "").se).ConfigureAwait(false) > 0;
|
||||
|
||||
////return await _dbContext.Set<T>().IgnoreQueryFilters().Where(where).ExecuteUpdateAsync(propValues.SetProperties<T>()).ConfigureAwait(false) > 0;
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user