EF Core HasQueryFilter: Beyond Soft Deletes
These articles are AI-generated summaries. Please check the original sources for full details.
Introduction EF Core has the HasQueryFilter extension method
EF Core’s HasQueryFilter extension method allows for flexible data filtering. This method is particularly useful in scenarios requiring soft deletion, where developers need to view data without a filter.
Why This Matters
The technical reality of data filtering in EF Core is that it can be complex and inflexible, especially when dealing with soft deletes. Ideal models would allow for easy filtering and unfiltering of data, but in reality, this can be difficult to implement. The HasQueryFilter extension method provides a solution to this problem, allowing developers to easily filter data and switch between filtered and unfiltered views.
Key Insights
- EF Core’s HasQueryFilter extension method can be used to filter data based on properties, such as country code or category ID.
- The HasQueryFilter method can be used in conjunction with the IgnoreQueryFilters method to view data without a filter.
- EF Core 10 introduces named query filters, which allow for more fine-grained control over data filtering.
Working Examples
Example of using HasQueryFilter to filter customers by country code
modelBuilder.Entity<Customer>().HasQueryFilter(c => EF.Property<int?>(c, nameof(Models.Customer.CountryIdentifier)) == 20);
Example of using HasQueryFilter to filter categories by ID
if (ContextSettings.Instance.CategoryOptions.UseQueryFilter) { modelBuilder.Entity<Category>().HasQueryFilter(c => EF.Property<int?>(c, nameof(Models.Category.CategoryId)) == ContextSettings.Instance.CategoryOptions.Id); }
Practical Applications
- Use case: A company like Amazon could use EF Core’s HasQueryFilter method to filter customer data by country code, allowing them to easily view data for specific regions. Pitfall: If not implemented correctly, data filtering can lead to inconsistent or incorrect results.
- Use case: A system like Netflix could use EF Core’s HasQueryFilter method to filter content by category, allowing them to easily recommend content to users. Pitfall: If not optimized, data filtering can lead to performance issues.
References:
Continue reading
Next article
Building Trustworthy AI Agents with Human-in-the-Loop Approval
Related Content
7 Code Quality Checkers for Vibecoded Projects: AI-Generated Code Needs Its Own Audit Stack
Inithouse tested 7 tools on vibecoded projects; Audit Vibe Coding catches structural debt linters miss.
LLM Solves Novel Dot Puzzle: What Next-Token Prediction Gets Wrong
Engineer reveals how an LLM solved a novel dot puzzle, challenging the 'next-token prediction' folk model and exposing emergent reasoning via attention mechanisms.
How One Developer Cut AI Agent Token Waste by 20K Per Query With a Simple Skill Pattern
Developer cuts AI token waste by 20k per query by replacing repeated agent reasoning with reusable skills, verified with real API tests.