Explore Global Query Filters in EF Core
In this article we are going to check one of the features of Entity Framework Core, Global Query Filters; this was introduced first in EF Core 2.0 and is just a query predicate that will be appended to the end of where clause for queries on entities for which this feature has been activated. Some common scenarios would be Soft delete
and Multi tenancy
.
Consider that you are writing an application in which entities can be softly deleted, why not completely delete those entities? Just don’t do that, said Udi Dahan. Okay, let’s create an Author
entity:
If we want to add a query filter to the Author
entity, that should be done either in OnModelCreating
method of the DbContext
, or in the EntityTypeConfiguration<T>
` class related to entity T
. For now, we could create a DbContext
and override its OnModelCreating
method, then configure the Author
entity by telling the context to automatically filter out soft-deleted records, their IsDeleted
property is true
.
To achieve that, HasQueryFilter
method is being used, it is defined on EntityTypeBuilder
and takes an Expression
of Func<Author, bool>
which means it will take an author instance and returns a boolean based on the conditions defined, in this case, the author not being (soft) deleted. Now, whenever you query the Author
entity, this query will also get appended to whatever you have provided for your where clause by the DbContext.
Resulting in a database query:
So far, so good. This could be a common scenario for almost all of your entities(aggregate roots, if interested) and we are reluctant to repeat the same code for every individual entity, Don’t Repeat Yourself.
Let’s first extract an interface, ICanBeSoftDeleted
, which desired entities will implement that.
Now we have to configure the common query filter for each entity, in the previous version we used a generic overload of modelBuilder.Entity<T>
method, now it is not possible though, so we have to generate a Lambda Expression for each entity to be able to use the non-generic overload:
Let’s discover the GenerateQueryFilterLambda
, it takes the type of the entity and will return a LambdaExpression
of Func<Type, bool>
, we should generate e => e.IsDeleted == false
, right? See how to generate each part using Expressions in .Net Core.
Comments on top of each line indicating that what part of the (lambda) expression is going to be generated. This way we can simply implement the interface and our DbContext automatically detect and add the common global filter to our queries for the specified entities. At the end, whenever you want to disable query filter use IgnoreQueryFilters()
on your LINQ query.
Conclusion
Most of the time there are business query patterns that will apply globally on some entities in your application, by employing Query Filters
of EF Core you could simply and easily implement such requirement. There is also a limitation, these filters can only be applied to the root entity of an inheritance hierarchy. Finally, you could find the source code for this article on GitHub. Have a great day and enjoy coding.