.NET EF Core: Global Query Filters

.NET EF Core: Global Query Filters

Global query filters allow attaching a filter to an entity type and having that filter applied whenever a query on that entity type is executed; think of them as an additional LINQ Where operator that’s added whenever the entity type is queried. Such filters are useful in a variety of cases.

See Global Query Filters

Specific examples are given for:

  • Soft deletion with IsDeleted column to filter soft-deleted by default in all queries.
  • Multi-tenant data with TenantId column to filter for specific tenant, and requiring tenant to be specified when creating DbContext like: var context = new MyDbContext(tenantId).