Skip to content

Instantly share code, notes, and snippets.

View daniel-chenery's full-sized avatar

Daniel Chenery daniel-chenery

View GitHub Profile
@davepcallan
davepcallan / globalQueryFilters.cs
Last active February 28, 2024 05:26
Apply global query filters automatically to all entities which implement an interface or have a particular property
public static void ApplyGlobalFilters<TInterface>(this ModelBuilder modelBuilder,
Expression<Func<TInterface,
bool>> expression)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (entityType.ClrType.GetInterface(typeof(TInterface).Name) != null)
{
var newParam = Expression.Parameter(entityType.ClrType);