From 3603656a1c97f254deee7a7fecd63bce252c362b Mon Sep 17 00:00:00 2001
From: hang <872297557@qq.com>
Date: Mon, 11 Nov 2024 18:09:18 +0800
Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E8=BF=87=E6=BB=A4=E4=BF=AE?=
=?UTF-8?q?=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Interceptor/SoftDeleteQueryExtension.cs | 52 +++++++++++++++++--
1 file changed, 49 insertions(+), 3 deletions(-)
diff --git a/IRaCIS.Core.Infra.EFCore/Interceptor/SoftDeleteQueryExtension.cs b/IRaCIS.Core.Infra.EFCore/Interceptor/SoftDeleteQueryExtension.cs
index d7acc11a5..c042f5051 100644
--- a/IRaCIS.Core.Infra.EFCore/Interceptor/SoftDeleteQueryExtension.cs
+++ b/IRaCIS.Core.Infra.EFCore/Interceptor/SoftDeleteQueryExtension.cs
@@ -1,20 +1,44 @@
using IRaCIS.Core.Domain.Models;
using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.Reflection;
namespace IRaCIS.Core.Infra.EFCore
{
+ ///
+ /// modelBuilder.Entity().HasQueryFilter(s => !s.IsDeleted);(这个是自动全局配置的)
+ /// modelBuilder.Entity().HasQueryFilter(c => !c.SubjectVisit.IsDeleted); (这个没有自动加) 所以有警告
+ ///
public static class SoftDeleteQueryExtension
{
public static void AddSoftDeleteQueryFilter(
- this IMutableEntityType entityData)
+ this IMutableEntityType entityType)
{
var methodToCall = typeof(SoftDeleteQueryExtension)
.GetMethod(nameof(GetSoftDeleteFilter),
BindingFlags.NonPublic | BindingFlags.Static)
- .MakeGenericMethod(entityData.ClrType);
+ .MakeGenericMethod(entityType.ClrType);
var filter = methodToCall.Invoke(null, new object[] { });
- entityData.SetQueryFilter((LambdaExpression)filter);
+ entityType.SetQueryFilter((LambdaExpression)filter);
+
+ //foreach (var navigation in entityType.GetNavigations())
+ //{
+ // var targetEntityType = navigation.TargetEntityType;
+
+ // // 如果目标实体实现了 ISoftDelete 接口,递归添加过滤器
+ // if (typeof(ISoftDelete).IsAssignableFrom(targetEntityType.ClrType))
+ // {
+ // // 在当前实体的导航属性上添加过滤器
+ // var targetEntityFilterMethod = typeof(SoftDeleteQueryExtension)
+ // .GetMethod(nameof(GetSoftDeleteFilterForNavigation),
+ // BindingFlags.NonPublic | BindingFlags.Static)
+ // .MakeGenericMethod(entityType.ClrType, targetEntityType.ClrType);
+
+ // var navigationFilter = targetEntityFilterMethod.Invoke(null, new object[] { navigation });
+ // entityType.SetQueryFilter((LambdaExpression)navigationFilter);
+ // }
+ //}
+
}
@@ -24,5 +48,27 @@ namespace IRaCIS.Core.Infra.EFCore
Expression> filter = x => !x.IsDeleted;
return filter;
}
+
+ // 获取导航属性的软删除过滤器
+ private static LambdaExpression GetSoftDeleteFilterForNavigation(
+ IMutableNavigation navigation)
+ where TEntity : class
+ where TNavigationEntity : class, ISoftDelete
+ {
+ // 获取导航属性的表达式
+ var parameter = Expression.Parameter(typeof(TEntity), "e");
+ var navigationExpression = Expression.Property(parameter, navigation.Name);
+
+ // 获取导航属性中的 IsDeleted 属性
+ var isDeletedProperty = Expression.Property(navigationExpression, nameof(ISoftDelete.IsDeleted));
+
+ // 生成过滤条件:导航属性不为空且未被软删除
+ var condition = Expression.AndAlso(
+ Expression.NotEqual(navigationExpression, Expression.Constant(null, navigationExpression.Type)), // 判断导航属性是否为null
+ Expression.Equal(isDeletedProperty, Expression.Constant(false)) // 判断导航属性的 IsDeleted 为 false
+ );
+
+ return Expression.Lambda>(condition, parameter);
+ }
}
}