Advanced EF Core Features Overview
EF Core provides a rich set of extensibility points that go beyond basic CRUD operations. These advanced features let you hook into the EF Core pipeline to add cross-cutting concerns like auditing, diagnostics, and data lifecycle management.
Why Advanced Features Matter
As applications grow, you encounter requirements that do not fit neatly into standard entity operations:
- Auditing: Automatically recording who created or modified a record and when
- Diagnostics: Logging the SQL that EF Core generates so you can troubleshoot performance issues
- Data retention: Keeping historical records instead of permanently deleting data
- Cross-cutting logic: Applying the same behavior to every SaveChanges call without duplicating code
EF Core addresses these needs through interceptors, global query filters, and diagnostic APIs.
Interceptors
Interceptors let you hook into EF Core operations at specific points in their lifecycle. There are two main categories:
SaveChanges Interceptors
ISaveChangesInterceptor lets you run logic before and after SaveChanges is called. Common uses:
- Setting
CreatedAtandUpdatedAttimestamps automatically - Recording the user who made the change
- Validating business rules before data reaches the database
csharp1public class AuditInterceptor : SaveChangesInterceptor2{3 public override InterceptionResult<int> SavingChanges(4 DbContextEventData eventData,5 InterceptionResult<int> result)6 {7 // Runs before SaveChanges commits8 SetAuditFields(eventData.Context!);9 return result;10 }11}
Command Interceptors
IDbCommandInterceptor lets you inspect or modify the SQL commands that EF Core sends to the database. This is useful for:
- Logging every SQL query for diagnostics
- Measuring query execution time
- Adding query hints or comments to SQL
Diagnostic Listeners
EF Core publishes diagnostic events through .NET's DiagnosticSource infrastructure. You can subscribe to these events for detailed telemetry without modifying your DbContext code. For simpler needs, the LogTo() method provides quick console-based SQL logging.
csharp1options.LogTo(Console.WriteLine, LogLevel.Information);
Soft Delete Pattern
Instead of permanently removing rows from the database, the soft delete pattern marks records as deleted by setting an IsDeleted flag. This preserves data for auditing, compliance, or recovery.
EF Core supports this pattern through global query filters, which automatically exclude soft-deleted records from all queries:
csharp1modelBuilder.Entity<BlogPost>()2 .HasQueryFilter(p => !p.IsDeleted);
When you query for blog posts, EF Core appends WHERE IsDeleted = 0 to every query. You can bypass the filter with IgnoreQueryFilters() when you need to access deleted records.
Temporal Tables
Temporal tables (also called system-versioned tables) automatically track the full history of every row. SQL Server has native support for this, and EF Core can configure temporal table mappings:
csharp1modelBuilder.Entity<Product>()2 .ToTable("Products", b => b.IsTemporal());
This lets you query data as it existed at any point in time, which is valuable for regulatory compliance, auditing, and debugging data issues.
What You Will Learn in This Topic
In the following lessons, you will:
- Implement SaveChanges interceptors to add automatic audit fields to your entities
- Use command interceptors and logging to inspect the SQL that EF Core generates
- Build a soft delete system with global query filters that transparently hides deleted records
- Understand temporal tables and when they are the right solution
- Combine these patterns in a hands-on workshop that builds an auditable entity system
Summary
- Interceptors hook into the EF Core pipeline to add cross-cutting behavior
- SaveChanges interceptors are ideal for audit fields and validation
- Command interceptors and diagnostic APIs help with SQL logging and performance analysis
- Soft delete uses a flag and global query filters instead of permanent deletion
- Temporal tables provide built-in historical data tracking at the database level
These features elevate your EF Core usage from basic data access to a production-ready data layer with auditing, diagnostics, and safe data management.