如何使用Entity Framework Core ExecuteUpdate方法更新影子属性?
解决方案:合并ExecuteUpdate表达式自动更新影子属性
要实现批量更新时自动附加ModifiedOn属性的更新,核心是通过表达式树合并技术,将自定义的属性更新逻辑与影子属性更新逻辑链式组合。
实现步骤
1. 表达式替换工具类
通过ExpressionVisitor实现表达式内的参数替换,用于将两个SetPropertyCalls的调用逻辑串联起来:
private class ExpressionReplacer : ExpressionVisitor { private readonly Expression _oldExpr; private readonly Expression _newExpr; public ExpressionReplacer(Expression oldExpr, Expression newExpr) { _oldExpr = oldExpr; _newExpr = newExpr; } public override Expression Visit(Expression node) { return node == _oldExpr ? _newExpr : base.Visit(node); } }
2. 扩展方法与表达式合并逻辑
封装ExecuteUpdateWithShadowPropertiesAsync扩展方法,合并用户自定义更新逻辑和ModifiedOn属性更新:
public static class EntityFrameworkExtensions { // 带系统时钟依赖的重载(与SaveChangesAsync逻辑保持时间一致性) public static Task<int> ExecuteUpdateWithShadowPropertiesAsync<TSource>( this IQueryable<TSource> source, Expression<Func<SetPropertyCalls<TSource>, SetPropertyCalls<TSource>>> setPropertyCalls, ISystemClock systemClock, CancellationToken cancellationToken = default) where TSource : BaseIdentifierEntity { var timestamp = systemClock.UtcNow.DateTime; Expression<Func<SetPropertyCalls<TSource>, SetPropertyCalls<TSource>>> setShadowPropertyCalls = calls => calls.SetProperty(p => p.ModifiedOn, timestamp); var mergedPropertyCalls = Merge(setPropertyCalls, setShadowPropertyCalls); return source.ExecuteUpdateAsync(mergedPropertyCalls, cancellationToken); } // 无系统时钟依赖的简化重载 public static Task<int> ExecuteUpdateWithShadowPropertiesAsync<TSource>( this IQueryable<TSource> source, Expression<Func<SetPropertyCalls<TSource>, SetPropertyCalls<TSource>>> setPropertyCalls, CancellationToken cancellationToken = default) where TSource : BaseIdentifierEntity { return ExecuteUpdateWithShadowPropertiesAsync(source, setPropertyCalls, new SystemClock(), cancellationToken); } private static Expression<Func<SetPropertyCalls<TSource>, SetPropertyCalls<TSource>>> Merge<TSource>( Expression<Func<SetPropertyCalls<TSource>, SetPropertyCalls<TSource>>> expr1, Expression<Func<SetPropertyCalls<TSource>, SetPropertyCalls<TSource>>> expr2) { // 将第二个表达式的参数替换为第一个表达式的执行结果,形成链式调用 var replacedExpr2Body = new ExpressionReplacer(expr2.Parameters[0], expr1.Body).Visit(expr2.Body); return Expression.Lambda<Func<SetPropertyCalls<TSource>, SetPropertyCalls<TSource>>>( replacedExpr2Body, expr1.Parameters); } }
使用示例
调用扩展方法即可完成批量更新并自动更新ModifiedOn:
// 带系统时钟的调用(推荐,与SaveChangesAsync时间逻辑统一) await context.Invoices .Where(_ => _.Status == InvoiceStatusEnum.Draft) .ExecuteUpdateWithShadowPropertiesAsync( _ => _.SetProperty(invoice => invoice.Status, InvoiceStatusEnum.Approved), systemClock, cancellationToken); // 简化调用 await context.Invoices .Where(_ => _.Status == InvoiceStatusEnum.Draft) .ExecuteUpdateWithShadowPropertiesAsync( _ => _.SetProperty(invoice => invoice.Status, InvoiceStatusEnum.Approved), cancellationToken);
原理说明
- 表达式合并逻辑会将用户传入的
SetProperty调用与ModifiedOn的更新调用串联,最终生成形如calls => calls.SetProperty(...) .SetProperty(p => p.ModifiedOn, timestamp)的链式表达式。 - EF Core会解析合并后的表达式,生成包含所有指定属性更新的批量SQL语句,确保影子属性与业务属性一同被更新。
内容的提问来源于stack exchange,提问作者Boomit
相关产品推荐
相关产品推荐

