first commit
This commit is contained in:
31
Complex.Infrastructure/Extensions/QueryFilter.cs
Normal file
31
Complex.Infrastructure/Extensions/QueryFilter.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Complex.Infrastructure.Extensions
|
||||
{
|
||||
public static class QueryFilter
|
||||
{
|
||||
public static void ApplyGlobalFilters<T>(this ModelBuilder modelBuilder,
|
||||
string propertyName, T value)
|
||||
{
|
||||
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
||||
{
|
||||
var foundProperty = entityType.FindProperty(propertyName);
|
||||
|
||||
if (foundProperty != null && foundProperty.ClrType == typeof(T))
|
||||
{
|
||||
var newParam = Expression.Parameter(entityType.ClrType);
|
||||
var filter = Expression.Lambda(Expression.Equal(Expression.Property(newParam, propertyName),
|
||||
Expression.Constant(value)), newParam);
|
||||
|
||||
modelBuilder.Entity(entityType.ClrType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user