Named global query filters in Entity Framework Core 10
Previous version of Entity Framework included a way to define global query filters while configuring the model. These filters were applied to all queries for a given entity type, allowing developers to implement features like soft deletes easily. However, this feature was limited in flexibility, as it did not allow multiple filters.
With Entity Framework 10, a new feature has been introduced that allows developers to define named global query filters. This enhancement provides greater flexibility by enabling multiple filters to be defined, which can be selectively disabled based on their names.
For the entire change list of new features in Entity Framework 10, refer to the official documentation.
Before: Single Global Query Filter link
In previous versions, you could define a single global query filter using the HasQueryFilter method. For example, to implement a soft delete feature, you might have done something like this:
The filter automatically excludes any Customer entities marked as deleted from all queries. If you wanted to include an additional filter, you would have to combine it with the existing one. For example, let's say you wanted to filter customers based on their status as well:
For scenarios where you needed to disable the global filter in specific queries, you had to use the IgnoreQueryFilters method, which disabled all global filters for that query:
Now: Named Global Query Filters link
With Entity Framework Core 10, you can now define named global query filters. This allows you to create multiple filters for the same entity type and selectively disable them by name (one or more).
The refactored code for the previous example would look like this.
Instead of combining multiple conditions into a single filter, invoke HasQueryFilter multiple times and provide a unique name for each filter.
It's still possible to create a global filter without a name, which behaves as before. But, you can't use both named and unnamed filters for the same entity type.
Now to disable specific filters in a query, you can use the IgnoreQueryFilters method and provide the name(s) of the filters you want to disable.
Feel free to update this blog post on GitHub, thanks in advance!
Join My Newsletter (WIP)
Join my weekly newsletter to receive my latest blog posts and bits, directly in your inbox.
Support me
I appreciate it if you would support me if have you enjoyed this post and found it useful, thank you in advance.