EF Core 10: Query Filters Just Got Smarter


One of the most underrated features in Entity Framework Core is also one of its most powerful: query filters.
If you’ve ever needed to build applications with multi-tenancy, soft deletes, role-based visibility, or data isolation, query filters are very perfect for you.
What Are Query Filters?
Query filters allow you to define global conditions that are automatically applied to every query involving a given entity.
Think of it as setting up rules once and letting EF Core enforce them forever.
Here’s a simple example:
[csharp]
modelBuilder.Entity<Post>()
.HasQueryFilter(p => !p.IsDeleted);
Now every query on the Post entity will automatically exclude soft-deleted items; no need to remember to add WHERE IsDeleted = false manually.
Why They’re a Game-Changer;
Query filters in EF Core aren't just a convenient feature, they’re a structural improvement that impacts the security, cleanliness, and scalability of your codebase.
Here’s why:
Security by Design
Instead of trusting every developer to remember to add tenant-specific filters or exclusion flags (e.g., IsDeleted == false), you enforce that logic at the model level. This means no accidental data exposure and no forgetting edge-case filters during a hotfix.
"Define it once, apply it forever."
Cleaner, DRY-er Code
Imagine a large codebase where every query on User, Post, or Order needs the same filter. Without query filters, you're repeating the same where clause everywhere. One missed spot? Hello, bug.
Query filters centralise that logic, eliminating duplication and reducing human error.
Enforced Business Rules
Query filters act like guardrails: they ensure that business logic like soft deletes, tenant isolation, or user access levels are consistently applied, regardless of who writes the query.
What’s New in EF Core 10?
With EF Core 10, Microsoft took an already powerful feature and made it far more dynamic and context-aware:
1. Context Injection for Dynamic Filters
You can now inject services like IUserContext, HttpContext, or ITenantProvider into your DbContext, allowing filters that adapt based on who's querying or what session they’re in.
[csharp]
modelBuilder.Entity<Order>()
.HasQueryFilter(o => o.TenantId == _currentUserContext.TenantId);
This is huge for multi-tenant SaaS platforms.
2. Smarter Translation to SQL
EF Core 10 has better support for translating complex filter expressions into clean, performant SQL. You can now include navigation properties, Enums, and nullable types with fewer surprises.
3. Support for Conditional Filters
You can make filters more intelligent by applying conditions like:
[csharp]
modelBuilder.Entity<Document>()
.HasQueryFilter(d => currentUser.IsAdmin || d.OwnerId == currentUser.Id);
This opens up role-based access logic directly within your data model.
Real-World Use Cases
Let’s look at how teams can use query filters practically:
1. Soft Deletes
Mark records as deleted (IsDeleted = true) without physically removing them. Query filters automatically hide them from results while retaining the data.
2. Multi-Tenancy
Restrict queries to the current tenant by applying a global filter like TenantId == CurrentTenantId. No risk of cross-tenant data leaks.
3. Role-Based Content Access
Hide or show data based on a user’s access level. Admins may see everything, but standard users only see what's assigned to them.
4. Content Lifecycle Filtering
Automatically hide archived, expired, or future-dated content without cluttering your code with where clauses everywhere.
5. GDPR / Data Residency Controls
In more advanced use cases, filters can help enforce region-specific visibility rules for compliance.
The Takeaway?
EF Core 10’s query filters are more than just a nice feature, they’re a mindset shift.
They help you:
Scale your application securely
Enforce rules consistently
Reduce duplication
Adapt your queries dynamically
Focus more on features, less on plumbing
If your app has more than one user type, deals with soft deletes, or has to enforce data boundaries, you should be using query filters.
And now that EF Core 10 gives you more power, flexibility, and runtime context, there’s really no excuse not to.
Subscribe to my newsletter
Read articles from Andrew directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
