Entity Framework Core – Include Filters

The Entity Framework Core (EF) extension method Include provides us the ability to load additional data besides the entities we are querying for. For example: loading products along with their translations.

In diesem Artikel:

pg
Pawel Gerr ist Architekt und Consultant bei Thinktecture. Er hat sich auf .NET Core Backends spezialisiert und kennt Entity Framework von vorne bis hinten.

The Entity Framework Core (EF) extension method Include provides us the ability to load additional data besides the entities we are querying for. For example: loading products along with their translations.

				
					var products = Context.Products
                      .Include(p => p.Translations)
                      .ToList();

--------------------------------------------------------------

public class Product
{
  public Guid Id { get; set; }
  public string Name { get; set; }
  public List<ProductTranslation> Translations { get; set; }
}

public class ProductTranslation
{
   public Guid ProductId { get; set; }
   public string Locale { get; set; }
   public string Description { get; set; }
}
				
			

In some use cases we want to load all translations for the requested products and in some cases we don’t. There are plenty of times when we need translations for a specific locale (i.e. language) only but at the moment there is no built-in support for providing filters to entities being included. You may go to the corresponding github issue#1833 and vote for this feature to increase the priority of it.

Some of our options are:

Entity Framework Plus

Not much to say here. Install the nuget package and use the extension method IncludeFilter.

				
					var products = Context.Products
                     .IncludeFilter(p => p.Translations.Where(t => t.Locale == "en"))
                     .ToList();
				
			

Please note that the generated queries will be different than when using built-in features of EF! To be aware of that is helpful especially during performance optimization, like finding appropriate database indexes.

Global Query Filters

The global query filters were introduced in EF 2.0 and are primarily for realization of features like soft delete and multi-tenancy. Although the filters are coupled to the entity type(s) and not to specific queries, still, we may enable and disable them at any time. Furthermore, despite the name the filters don’t have to be „global“, i.e. we can enable them for a specific instance of a DbContext only and not for all of them.

First, we introduce a variable that will be our filter and configure the corresponding query filter.

				
					public class DemoDbContext : DbContext
{
   private string _localeFilter;

   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
      ...

      modelBuilder.Entity<ProductTranslation>()
                  .HasQueryFilter(t => _localeFilter == null 
                                       || t.Locale == _localeFilter);

   }
				
			

For setting the filter we could make the variable _localeFilter public but the API would not be that nice and robust, that’s why …

The method for enabling/setting the filter comes next.

				
					// we are still in DemoDbContext
   public IDisposable SetTranslationFilter(string locale)
   {
      if (locale == null)
         throw new ArgumentNullException(nameof(locale));
      if (_localeFilter != null)
         throw new InvalidOperationException($"Changing a filter is not allowed.");

      _localeFilter = locale;

      return new FilterReset(this);
   }
				
			

SetTranslationFilter is returning an IDisposable to not to forget to reset the filter. The FilterReset is a nested private struct inside DemoDbContext.

				
					private readonly struct FilterReset : IDisposable
   {
      private readonly DemoDbContext _ctx;

      public FilterReset(DemoDbContext ctx)
      {
         _ctx = ctx ?? throw new ArgumentNullException(nameof(ctx));
      }

      public void Dispose()
      {
         _ctx._localeFilter = null;
      }
   }
				
			

We are done with the implementation of the filter. The usage is looks as following: 

				
					using (Context.SetTranslationFilter(locale))
{
   var products = Context.Products
                         .Include(p => p.Translations)
                         .ToList();
}
				
			

When using the global query filters in that manner then it is impossible that this filter is applied to other queries by accident because (1) the filter is bound to a specific instance of a DbContext and (2) a DbContext is not thread-safe so it must not be used in multiple threads concurrently.

Kostenloser
Newsletter

Aktuelle Artikel, Screencasts, Webinare und Interviews unserer Experten für Sie

Verpassen Sie keine Inhalte zu Angular, .NET Core, Blazor, Azure und Kubernetes und melden Sie sich zu unserem kostenlosen monatlichen Dev-Newsletter an.

Newsletter Anmeldung
Diese Artikel könnten Sie interessieren
Database Access with Sessions
.NET
KP-round

Data Access in .NET Native AOT with Sessions

.NET 8 brings Native AOT to ASP.NET Core, but many frameworks and libraries rely on unbound reflection internally and thus cannot support this scenario yet. This is true for ORMs, too: EF Core and Dapper will only bring full support for Native AOT in later releases. In this post, we will implement a database access layer with Sessions using the Humble Object pattern to get a similar developer experience. We will use Npgsql as a plain ADO.NET provider targeting PostgreSQL.
15.11.2023
Old computer with native code
.NET
KP-round

Native AOT with ASP.NET Core – Overview

Originally introduced in .NET 7, Native AOT can be used with ASP.NET Core in the upcoming .NET 8 release. In this post, we look at the benefits and drawbacks from a general perspective and perform measurements to quantify the improvements on different platforms.
02.11.2023
.NET
KP-round

Optimize ASP.NET Core memory with DATAS

.NET 8 introduces a new Garbage Collector feature called DATAS for Server GC mode - let's make some benchmarks and check how it fits into the big picture.
09.10.2023
.NET CORE
pg

Incremental Roslyn Source Generators: High-Level API – ForAttributeWithMetadataName – Part 8

With the version 4.3.1 of Microsoft.CodeAnalysis.* Roslyn provides a new high-level API - the method "ForAttributeWithMetadataName". Although it is just 1 method, still, it addresses one of the biggest performance issue with Source Generators.
16.05.2023
AI
favicon

Integrating AI Power into Your .NET Applications with the Semantic Kernel Toolkit – an Early View

With the rise of powerful AI models and services, questions come up on how to integrate those into our applications and make reasonable use of them. While other languages like Python already have popular and feature-rich libraries like LangChain, we are missing these in .NET and C#. But there is a new kid on the block that might change this situation. Welcome Semantic Kernel by Microsoft!
03.05.2023
.NET
sg

.NET 7 Performance: Regular Expressions – Part 2

There is this popular quote by Jamie Zawinski: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."

In this second article of our short performance series, we want to look at the latter one of those problems.
25.04.2023