-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSimpleSoftDeleteQueryExtension.cs
34 lines (31 loc) · 1.3 KB
/
SimpleSoftDeleteQueryExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright (c) 2021 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.
using System;
using System.Linq.Expressions;
using System.Reflection;
using DataLayer.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace DataLayer.SingleEfCode
{
public static class SimpleSoftDeleteQueryExtension
{
public static void AddSingleSoftDeleteQueryFilter(
this IMutableEntityType entityData)
{
var methodToCall = typeof(SimpleSoftDeleteQueryExtension)
.GetMethod(nameof(GetSoftDeleteFilter),
BindingFlags.NonPublic | BindingFlags.Static)
.MakeGenericMethod(entityData.ClrType);
var filter = methodToCall.Invoke(null, new object[] { });
entityData.SetQueryFilter((LambdaExpression)filter);
entityData.AddIndex(entityData.FindProperty(nameof(ISingleSoftDelete.SoftDeleted)));
}
private static LambdaExpression GetSoftDeleteFilter<TEntity>()
where TEntity : class, ISingleSoftDelete
{
Expression<Func<TEntity, bool>> filter = x => !x.SoftDeleted;
return filter;
}
}
}