using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.ChangeTracking; using System.Linq; public class MyDbContext : DbContext { public DbSetMyEntities { get; set; } public override int SaveChanges() { var modifiedEntries = ChangeTracker.Entries() .Where(x => x.Entity is IAuditableEntity && (x.State == EntityState.Added || x.State == EntityState.Modified)); foreach (var entry in modifiedEntries) { var entity = (IAuditableEntity)entry.Entity; DateTime now = DateTime.UtcNow; if (entry.State == EntityState.Added) { entity.CreatedDate = now; } else { base.Entry(entity).Property(x => x.CreatedDate).IsModified = false; } entity.ModifiedDate = now; } return base.SaveChanges(); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity (entity => { entity.ToTable("MyEntities"); entity.ApplyAuditTrail(); // <-- Adding the audit trail entity.HasKey(e => e.Id); }); } } public interface IAuditableEntity { DateTime CreatedDate { get; set; } DateTime ModifiedDate { get; set; } } public class MyEntity : IAuditableEntity { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public DateTime CreatedDate { get; set; } public DateTime ModifiedDate { get; set; } }
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.ChangeTracking; using System.Linq; public class MyDbContext : DbContext { public DbSetIn this example, we have two entities, MyEntity and AnotherEntity, both of which implement the IAuditableEntity interface. In the OnModelCreating method, we configure both entities to have an audit trail by calling the ApplyAuditTrail method. Package library: Microsoft.EntityFrameworkCore.MyEntities { get; set; } public DbSet AnotherEntities { get; set; } public override int SaveChanges() { var modifiedEntries = ChangeTracker.Entries() .Where(x => x.Entity is IAuditableEntity && (x.State == EntityState.Added || x.State == EntityState.Modified)); foreach (var entry in modifiedEntries) { var entity = (IAuditableEntity)entry.Entity; DateTime now = DateTime.UtcNow; if (entry.State == EntityState.Added) { entity.CreatedDate = now; } else { base.Entry(entity).Property(x => x.CreatedDate).IsModified = false; } entity.ModifiedDate = now; } return base.SaveChanges(); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity (entity => { entity.ToTable("MyEntities"); entity.ApplyAuditTrail(); entity.HasKey(e => e.Id); }); modelBuilder.Entity (entity => { entity.ToTable("AnotherEntities"); entity.ApplyAuditTrail(); entity.HasKey(e => e.Id); }); } } public interface IAuditableEntity { DateTime CreatedDate { get; set; } DateTime ModifiedDate { get; set; } } public class MyEntity : IAuditableEntity { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public DateTime CreatedDate { get; set; } public DateTime ModifiedDate { get; set; } } public class AnotherEntity : IAuditableEntity { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public DateTime CreatedDate { get; set; } public DateTime ModifiedDate { get; set; } }