using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using Z.EntityFramework.Plus;
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
AuditManager.DefaultConfiguration
.AuditEntryFactory = args =>
new CustomAuditEntry() { Time_CustomAuditEntry = DateTime.Now };
AuditManager.DefaultConfiguration
.AuditEntryPropertyFactory = args =>
new CustomAuditEntryProperty() { Text_CustomAuditEntryProperty = "Text" };
AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) =>
(context as EntityContext).CustomAuditEntries.AddRange(audit.Entries.Cast<CustomAuditEntry>());
using (var context = new EntityContext())
context.Customers.Add(new Customer() { Name = "Customer_C", Description = "Description", IsActive = false});
audit.CreatedBy = "ZZZ Projects";
context.SaveChanges(audit);
using (var context = new EntityContext())
FiddleHelper.WriteTable("All Entry",context.CustomAuditEntries.ToList());
foreach(var entry in context.CustomAuditEntries.ToList())
FiddleHelper.WriteTable("Properties for Entry ID: " + entry.AuditEntryID,context.CustomAuditEntryProperties.Where(x => x.AuditEntryID == entry.AuditEntryID).ToList());
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
public DbSet<Customer> Customers { get; set; }
public DbSet<CustomAuditEntry> CustomAuditEntries { get; set; }
public DbSet<CustomAuditEntryProperty> CustomAuditEntryProperties { get; set; }
public int CustomerID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Boolean IsActive { get; set; }
public class CustomAuditEntry : AuditEntry
public DateTime Time_CustomAuditEntry {get;set;}
public class CustomAuditEntryProperty : AuditEntryProperty
public String Text_CustomAuditEntryProperty {get;set;}