

ModelBuilder.Entity().HasQueryFilter(p => p.Title.Contains("fish")) ModelBuilder.Entity().HasQueryFilter(b => b.Posts.Count > 0) To illustrate this configure query filters in OnModelCreating in the following way: modelBuilder.Entity().HasMany(b => b.Posts).WithOne(p => p.Blog) When EF Core expands navigations used in query filters, it will also apply query filters defined on referenced entities. Using navigations in query filter will cause query filters to be applied recursively. You can also use navigations in defining global query filters. However, you can define a single filter with multiple conditions using the logical AND operator ( & in C#). It is currently not possible to define multiple query filters on the same entity - only the last one will be applied. The predicate expressions passed to the HasQueryFilter calls will now automatically be applied to any LINQ queries for those types. ModelBuilder.Entity().HasQueryFilter(p => !p.IsDeleted) modelBuilder.Entity().HasQueryFilter(b => EF.Property(b, "_tenantId") = _tenantId)

Next, configure the query filters in OnModelCreating using the HasQueryFilter API. That is, the instance is marked as deleted without physically removing the underlying data. This property is used to keep track of whether a post instance has been "soft-deleted". Also defined is an IsDeleted property on the Post entity type. This field will be used to associate each Blog instance with a specific tenant. Note the declaration of a _tenantId field on the Blog entity.

#pragma warning restore IDE0051, CS0169 // Remove unused private members #pragma warning disable IDE0051, CS0169 // Remove unused private members There is also an article with comprehensive guidance for multi-tenancy in EF Core applications.įirst, define the entities: public class Blog Multi-tenancy is used here as a simple example.
