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 soft deleted, why not completely delete those entities? just don’t do that, said Udi Dahan. Okay, let’s create an Author entity:

1
2
3
4
5
6
7
8
9
10
public class Author
{
// omitted code

public Guid Id { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
public DateTime DateOfBirth { get; private set; }
public bool IsDeleted { get; private set; }
}

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class BookstoreDbContext : DbContext
{
// omitted code

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Author>()
.HasQueryFilter(author => !author.IsDeleted);

base.OnModelCreating(modelBuilder);
}

public DbSet<Author> Authors { get; set; }
}

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.

1
2
3
// omitted code
var authors = context.Authors
.Where(author => author.LastName.StartsWith("joh"));

resulting in a database query:

1
2
3
4
SELECT  a.*
FROM dbo.Authors as a
WHERE a.LastName LIKE 'joh%'
AND a.IsDelete = 0

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 same code for every individual entity(DRY).

Let’s first extract an interface, ICanBeSoftDeleted, which desired entities will implement that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public interface ICanBeSoftDeleted
{
bool IsDeleted{ get; set;}
}

public class Author : ICanBeSoftDeleted
{
// omitted code
}

public class Book : ICanBeSoftDeleted
{
// omitted code
}

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var softDeleteEntities = typeof(ICanBeSoftDeleted).Assembly.GetTypes()
.Where(type => typeof(ICanBeSoftDeleted)
.IsAssignableFrom(type)
&& type.IsClass
&& !type.IsAbstract);

foreach (var softDeleteEntity in softDeleteEntities)
{
modelBuilder.Entity(softDeleteEntity)
.HasQueryFilter(
GenerateQueryFilterLambda(softDeleteEntity)
);
}

base.OnModelCreating(modelBuilder);
}

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 private LambdaExpression GenerateQueryFilterLambda(Type type)
{
// we should generate: e => e.IsDeleted == false
// or: e => !e.IsDeleted

// e =>
var parameter = Expression.Parameter(type, "e");

// false
var falseConstant = Expression.Constant(false);

// e.IsDeleted
var propertyAccess = Expression.PropertyOrField(parameter, nameof(ICanBeSoftDeleted.IsDeleted));

// e.IsDeleted == false
var equalExpression = Expression.Equal(propertyAccess, falseConstant);

// e => e.IsDeleted == false
var lambda = Expression.Lambda(equalExpression, parameter);

return lambda;
}

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.

1
2
3
var authors = context.Authors
.Where(author => author.LastName.StartsWith("joh"))
.IgnoreQueryFilters();

Conclusion

Most of the times there are business query patterns that will apply globally on some entities in you 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, here you could find a sample for this article. Have a great day and enjoy coding.