36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using System.Linq.Expressions;
|
|
using System.Reflection;
|
|
using Bunny.Dao.Entity.Base;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
|
|
namespace Bunny.Common.Context;
|
|
|
|
/// <summary>
|
|
/// 软删除过滤器
|
|
/// </summary>
|
|
public static class SoftDeleteQueryExtension
|
|
{
|
|
/// <summary>
|
|
/// 软删除过滤器
|
|
/// </summary>
|
|
/// <param name="entityData"></param>
|
|
public static void AddSoftDeleteQueryFilter(this IMutableEntityType entityData)
|
|
{
|
|
var methodToCall = typeof(SoftDeleteQueryExtension)
|
|
.GetMethod(nameof(GetSoftDeleteFilter), BindingFlags.NonPublic | BindingFlags.Static)
|
|
?.MakeGenericMethod(entityData.ClrType);
|
|
var filter = methodToCall?.Invoke(null, []);
|
|
entityData.SetQueryFilter((LambdaExpression)filter!);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 软删除
|
|
/// </summary>
|
|
/// <typeparam name="TEntity"></typeparam>
|
|
/// <returns></returns>
|
|
private static LambdaExpression GetSoftDeleteFilter<TEntity>() where TEntity : BaseEntity
|
|
{
|
|
Expression<Func<TEntity, bool>> filter = x => !x.IsDeleted;
|
|
return filter;
|
|
}
|
|
} |