Global Query Filters & Owned Entities
Apply tenant isolation and soft-delete with global query filters, and map value objects using owned entity types.
What Are Global Query Filters?
Global query filters are WHERE predicates automatically applied to every LINQ query for a given entity type. They're perfect for soft delete, multi-tenancy, and row-level security — without repeating the condition everywhere.
Soft Delete with Global Filter
Add an IsDeleted flag and configure a global filter so deleted records are never returned unless you explicitly ignore the filter.
// Entity with soft-delete flag
public class Post
{
public int Id { get; set; }
public string Title { get; set; } = "";
public bool IsDeleted { get; set; }
}
// Configure in OnModelCreating
modelBuilder.Entity<Post>()
.HasQueryFilter(p => !p.IsDeleted);
// All queries now implicitly add WHERE IsDeleted = 0:
var posts = await _db.Posts.ToListAsync();All lessons in this course
- Eager, Lazy & Explicit Loading
- Raw SQL, Stored Procedures & Interpolation
- Compiled Queries & Performance
- Global Query Filters & Owned Entities