using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using Z.EntityFramework.Plus;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
AuditManager.DefaultConfiguration.ExcludeDataAnnotation();
AuditManager.DefaultConfiguration.DataAnnotationDisplayName();
AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) =>
audit.CreatedBy = "Kevin Lam";
var customAuditEntries = audit.Entries.Select(x => new DbAuditEntry(x)).ToList();
(context as EntityContext).AuditEntries.AddRange(customAuditEntries);
using (var context = new EntityContext())
context.Customers.Add(new Customer() { Name = "Customer_C", Description = "Description", IsActive = false});
context.Customers.Add(new Customer() { Name = "Customer_D", Description = "Description2", IsActive = true});
var c1 = context.Customers.FirstOrDefault();
var entries = context.AuditEntries;
FiddleHelper.WriteTable("All Entry",entries);
foreach(var entry in entries)
FiddleHelper.WriteTable("Properties for One Entry",entry.Properties);
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
public override int SaveChanges()
audit.CreatedBy="lamkek";
audit.PreSaveChanges(this);
var rowAffecteds = base.SaveChanges();
if (audit.Configuration.AutoSavePreAction != null)
audit.Configuration.AutoSavePreAction(this, audit);
public DbSet<DbAuditEntry> AuditEntries { get; set; }
public DbSet<DbAuditEntryProperty> AuditEntryProperties { get; set; }
public DbSet<Customer> Customers { get; set; }
public class DbAuditEntry
public DbAuditEntry(AuditEntry entry)
EntityTypeName = entry.EntityTypeName;
StateName = entry.StateName;
CreatedBy = entry.CreatedBy;
CreatedDate = entry.CreatedDate;
Properties = entry.Properties.Select(x => new DbAuditEntryProperty(x)).ToList();
public int AuditEntryID { get; set; }
public string EntityTypeName { get; set; }
public AuditEntryState State { get; set; }
public string StateName { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public List<DbAuditEntryProperty> Properties { get; set; }
public class DbAuditEntryProperty
public DbAuditEntryProperty()
public DbAuditEntryProperty(AuditEntryProperty property)
RelationName = property.RelationName;
PropertyName = property.PropertyName;
OldValue = property.OldValueFormatted;
NewValue = property.NewValueFormatted;
public int AuditEntryPropertyID { get; set; }
public int AuditEntryID { get; set; }
public string RelationName { get; set; }
public string PropertyName { get; set; }
public string OldValue { get; set; }
public string NewValue { get; set; }
[AuditDisplay("KevinTable")]
[AuditDisplay("Customer Identification")]
public int CustomerID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Boolean IsActive { get; set; }