skip to content

In EF Core 10, how do named query filters let a query skip the soft-delete filter but keep the tenant filter on forum posts?

level: middleimportance: should knowfreq 34%

answer

  1. second unnamed HasQueryFilter overwrites the first
  2. HasQueryFilter("key", lambda)
  3. IgnoreQueryFilters(["SoftDelete"])
  4. anonymous and named cannot mix on one type
  5. parameterless IgnoreQueryFilters disables all

basics

~10 s

EF Core 10 lets you register several filters per entity type under keys, HasQueryFilter("SoftDelete", ...) and HasQueryFilter("Tenant", ...), and IgnoreQueryFilters(["SoftDelete"]) disables only that one. Before 10, a second unnamed call overwrote the first.

solid answer

~40 s

Up to EF Core 9 an entity type had one filter: a second `HasQueryFilter(lambda)` call silently replaced the first, so soft delete and tenancy had to be combined with `&&`, and `IgnoreQueryFilters()` could only remove both. EF Core 10 adds **named filters**: `HasQueryFilter("SoftDelete", p => !p.IsDeleted)` and `HasQueryFilter("Tenant", p => p.TenantId == TenantId)` are stored separately. A moderator query then calls `IgnoreQueryFilters(["SoftDelete"])`, which takes an `IReadOnlyCollection<string>` of keys and removes only those filters, so the tenant filter stays and moderators never see another community's deleted posts. The parameterless `IgnoreQueryFilters()` still disables everything. One entity type cannot mix an anonymous filter with named ones; EF throws an `InvalidOperationException`. I keep the keys as constants so callers cannot mistype them.

code

csharp · 6 lines
csharp
// EF Core 10: moderators see deleted posts, but only in their own tenant
var queue = await db.Posts
    .IgnoreQueryFilters([ForumFilters.SoftDelete])
    .Where(p => p.IsDeleted && p.DeletedAt > since)
    .OrderBy(p => p.DeletedAt)
    .ToListAsync(ct);

go deeper

for a junior

Know that EF Core 10 lets you name query filters and switch off one of them with IgnoreQueryFilters and a list of names.

for a middle

Explain the pre-10 overwrite behaviour, the combined-predicate workaround, and how keyed ignores keep tenant isolation intact.

for a senior

Plan the migration from combined filters, share keys across entity types, and test that every bypass keeps tenant filters on.

for a principal

Set conventions for filter keys and bypass approvals so isolation rules stay auditable as the model grows.

## The problem before EF Core 10 The forum needs two filters on `Post`: hide soft-deleted posts and restrict to the current tenant. Before EF Core 10 an entity type could hold **one** filter, and a second call replaced the first: ```csharp modelBuilder.Entity<Post>().HasQueryFilter(p => !p.IsDeleted); modelBuilder.Entity<Post>().HasQueryFilter(p => p.TenantId == TenantId); // replaces the soft-delete filter ``` The workaround was a single combined predicate, `p => !p.IsDeleted && p.TenantId == TenantId`. That works until a moderator page needs deleted posts: `IgnoreQueryFilters()` removes the whole predicate, **including tenant isolation**, so the page must re-add the tenant condition by hand, and forgetting it leaks data across communities. ## Named filters EF Core 10 adds overloads that take a **filter key**: ```csharp public static class ForumFilters { public const string SoftDelete = nameof(SoftDelete); public const string Tenant = nameof(Tenant); } modelBuilder.Entity<Post>() .HasQueryFilter(ForumFilters.SoftDelete, p => !p.IsDeleted) .HasQueryFilter(ForumFilters.Tenant, p => p.TenantId == TenantId); ``` Each key is stored separately on the entity type; registering the same key again replaces only that filter, and passing `null` as the expression removes it. All named filters of the type are combined with `AND` when a query runs. ## Ignoring selected filters A new `IgnoreQueryFilters` overload takes the keys to skip: ```csharp var deletedHere = await db.Posts .IgnoreQueryFilters([ForumFilters.SoftDelete]) .Where(p => p.ThreadId == threadId && p.IsDeleted) .ToListAsync(ct); ``` The generated SQL still contains the tenant condition but no `IsDeleted` condition. Behaviour to remember: | Call | Effect | |---|---| | `IgnoreQueryFilters()` | disables **all** filters on every entity type in the query | | `IgnoreQueryFilters(["SoftDelete"])` | disables filters with that key on every entity type in the query | | a key no type uses | nothing is disabled; no error | Because keys are matched across the whole query, use the **same key name** for the same concern on every entity type, for example `SoftDelete` on `Post`, `Thread` and `Comment`. Then one ignore call is consistent across `Include`d data. ## Rules and errors 1. An entity type uses **either** one anonymous filter **or** named filters. Adding a named filter to a type that already has an anonymous one, or the reverse, throws an `InvalidOperationException` stating that anonymous and named filters cannot be combined. 2. Ignoring by key never disables an **anonymous** filter on another entity type in the same query; only the parameterless overload does. 3. Filters are still defined on the **root** of an inheritance hierarchy only. ## Where the keys are checked Filter keys are plain strings. EF does not validate the keys you pass to `IgnoreQueryFilters`: a misspelt key simply ignores nothing, and the soft-delete filter stays on. That failure is safe for tenancy but confusing for the moderator page, which then shows no deleted posts. Two habits prevent it: - keep keys as `const string` members of one static class and never type them inline; - cover each keyed bypass with a test that asserts the deleted rows **do** appear. ## Migrating an existing combined filter 1. Replace the combined `&&` predicate with one named filter per concern. 2. Replace `IgnoreQueryFilters()` calls that re-added the tenant condition with `IgnoreQueryFilters([SoftDelete])`, and delete the manual tenant condition. 3. Add a test per call site that asserts another tenant's rows stay invisible.

  • What happens if a Post already has HasQueryFilter(p => !p.IsDeleted) and you add HasQueryFilter("Tenant", ...)?
    EF throws an `InvalidOperationException`: an entity type can have either one anonymous filter or named filters, not both. Convert the existing filter to a named one, for example `HasQueryFilter("SoftDelete", p => !p.IsDeleted)`, and both can then coexist.
  • Why give the soft-delete filter the same key on Post, Thread and Comment?
    `IgnoreQueryFilters(["SoftDelete"])` matches keys across every entity type in the query. With one shared key, a moderator query that includes threads and comments switches off soft delete consistently while keeping every tenant filter. Different keys per type would leave some included data filtered.

  • Two unnamed HasQueryFilter calls on one type combine with AND.
  • IgnoreQueryFilters() in EF Core 10 skips only the soft-delete filter.
  • An entity type can mix an anonymous filter with named filters.
  • Named filters require a separate DbContext per filter combination.

Assumes EF Core 10, which introduced named query filters and IgnoreQueryFilters(IReadOnlyCollection<string>); EF Core 8 and 9 allowed one filter per entity type.