Entity Framework Core Interview Question #168
How can EF Core implement auditing?
Advanced Mapping, Interceptors, Value Conversions & Architecture Senior Advanced
Quick Interview Answer
Auditing can record who changed data, when it changed, and sometimes the before/after values. A common approach inspects ChangeTracker entries during SaveChanges or uses a SaveChanges interceptor. Audit records should be written consistently and should avoid leaking sensitive values that should not be retained.
Detailed Explanation
Auditing can record who changed data, when it changed, and sometimes the before/after values. A common approach inspects ChangeTracker entries during SaveChanges or uses a SaveChanges interceptor. Audit records should be written consistently and should avoid leaking sensitive values that should not be retained.
Code Example
foreach (var e in ChangeTracker.Entries<IAuditableEntity>())
{
if (e.State == EntityState.Modified)
e.Entity.UpdatedAtUtc = DateTime.UtcNow;
}