.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.
Specific examples are given for:
- Soft deletion with
IsDeletedcolumn to filter soft-deleted by default in all queries. - Multi-tenant data with
TenantIdcolumn to filter for specific tenant, and requiring tenant to be specified when creatingDbContextlike:var context = new MyDbContext(tenantId).