Introduction
EF Core 10 introduces named query filters, an improvement to global query filters that makes common patterns like soft deletion and multitenancy easier to manage. In earlier versions, each entity type effectively had one combined filter, which meant disabling filters for a special query was all-or-nothing. With EF Core 10, filters can be given individual names, such as SoftDeletionFilter or TenantFilter, allowing developers to define multiple filters on the same entity and selectively disable only the ones needed for a specific LINQ query. This gives applications more precise control while keeping the default filtering behavior clean, consistent, and centralized. (learn.microsoft.com)
Topics covered
- Creating filters
- Ignoring filters
- Using supplied language extensions for checking filter(s) exists for a model
Source code
- The provided source code is fully documented.
- SQL has been provided to create the database.
Source code samples Source code for language extension
Table schema for code samples
Employee schema, IsDeleted column for soft delete, IsManager indicates if the record is a manager.
Soft delete DbContext setup
SaveChanges event needs to be overridden so that a record is not removed, instead State is set to modified and IsDeleted column/property is set to true.
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
ChangeTracker.DetectChanges();
foreach (var entry in ChangeTracker.Entries())
{
if (entry.State == EntityState.Deleted)
{
// Change state to modified and set delete flag
entry.State = EntityState.Modified;
entry.Property("IsDeleted").CurrentValue = true;
}
}
return await base.SaveChangesAsync(cancellationToken);
}
public override int SaveChanges()
{
ChangeTracker.DetectChanges();
foreach (var entry in ChangeTracker.Entries())
{
if (entry.State == EntityState.Deleted)
{
// Change state to modified and set delete flag
entry.State = EntityState.Modified;
entry.Property("IsDeleted").CurrentValue = true;
}
}
return base.SaveChanges();
}
Filter setup
For the provided code sample, there is a filter for soft delete and a filter for if a record is a manager.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>(entity =>
{
entity.Property(e => e.FirstName)
.IsRequired()
.HasMaxLength(50);
entity.Property(e => e.LastName)
.IsRequired()
.HasMaxLength(50);
entity.HasQueryFilter("SoftDelete", e => !e.IsDeleted);
entity.HasQueryFilter("IsManager", e => e.IsManager);
});
OnModelCreatingPartial(modelBuilder);
}
Performing a soft delete
In this case the overridden SaveChangesAsync is used which changes the State from Deleted to Modified.
AnsiConsole.MarkupLine is from Spectre.Console
private static async Task PerformDelete()
{
SpectreConsoleHelpers.PrintPink();
int id = 2;
await using var context = new Context();
var employee = await context.Employees.FirstOrDefaultAsync(x => x.Id == id);
if (employee is not null)
{
context.Employees.Remove(employee).State = EntityState.Deleted;
var affected = await context.SaveChangesAsync();
AnsiConsole.MarkupLine(affected > 0
? $"[green]Successfully deleted employee with ID {id}.[/]"
: $"[red]Failed to delete employee with ID {id}. Affected rows: {affected}[/]");
}
else
{
AnsiConsole.MarkupLine($"[yellow]Employee with ID {id} not found.[/]");
}
}
Ignore a filter
using var context = new Context();
var employees = context.Employees
.IgnoreQueryFilters(["SoftDelete"])
.ToList();
Get query filter language extensions
These can be helpful when a developer doesn't have the DbContext source code.
public static class DbContextExtensions
{
extension(DbContext context)
{
public bool HasQueryFilter<TEntity>() where TEntity : class
{
var entityType = context.Model.FindEntityType(typeof(TEntity));
return entityType?.GetDeclaredQueryFilters() != null;
}
public IReadOnlyCollection<IQueryFilter>? GetQueryFilters<TEntity>() where TEntity : class
{
var entityType = context.Model.FindEntityType(typeof(TEntity));
return entityType?.GetDeclaredQueryFilters();
}
public IReadOnlyCollection<IQueryFilter> TryGetQueryFilters<TEntity>() where TEntity : class
{
var entityType = context.Model.FindEntityType(typeof(TEntity));
var filters = entityType?.GetDeclaredQueryFilters();
return filters ?? [];
}
}
}
One sample
private static void DisplayEmployeeQueryFilters()
{
using var context = new Context();
if (context.HasQueryFilter<Employee>())
{
var filters = context.GetQueryFilters<Employee>();
if (filters is null) return;
foreach (var (index, filter) in filters.Index())
{
AnsiConsole.MarkupLine($"{index, -4}" +
$"[cyan]Name[/] {filter.Key} " +
$"[cyan]Expression[/] {filter.Expression}");
}
}
else
{
AnsiConsole.MarkupLine("[red]No query filters found for Employee entity.[/]");
}
}
Summary
The EF Core team has made it easy to set multiple query filters, and by following the instructions and sample code provided, developers can use named query filters.


Top comments (0)