public async Task Test_EFDataProvider_AuditEntityDisabledAsync() { var dp = new EntityFrameworkDataProvider(); dp.AuditTypeMapper = t => { if (t == typeof(Order)) { return(typeof(OrderAudit)); } if (t == typeof(Orderline)) { return(typeof(OrderlineAudit)); } return(null); }; dp.AuditEntityAction = (ev, entry, obj) => { // return false to avoid saving return(false); }; Audit.Core.Configuration.Setup() .UseCustomProvider(dp); var id = Guid.NewGuid().ToString(); using (var ctx = new AuditPerTableContext()) { var o = new Order() { Number = id, Status = "Pending", OrderLines = new Collection <Orderline>() { new Orderline() { Product = "p1: " + id, Quantity = 2 }, new Orderline() { Product = "p2: " + id, Quantity = 3 } } }; await ctx.AddAsync(o); await ctx.SaveChangesAsync(); } using (var ctx = new AuditPerTableContext()) { var order = await ctx.Order.SingleAsync(a => a.Number.Equals(id)); var orderlineAudits = ctx.OrderlineAudit.AsNoTracking().Where(a => a.OrderId.Equals(order.Id)).ToList(); Assert.AreEqual(0, orderlineAudits.Count); } }
public async Task Test_EFDataProvider_AuditEntityDisabled_Fluent_Async() { Audit.Core.Configuration.Setup() .UseEntityFramework(config => config .AuditTypeMapper(typeName => Type.GetType(typeName + "Audit")) .AuditEntityAction((ev, ent, audEnt) => { return(false); }) ); var id = Guid.NewGuid().ToString(); using (var ctx = new AuditPerTableContext()) { var o = new Order() { Number = id, Status = "Pending", OrderLines = new Collection <Orderline>() { new Orderline() { Product = "p1: " + id, Quantity = 2 }, new Orderline() { Product = "p2: " + id, Quantity = 3 } } }; await ctx.AddAsync(o); await ctx.SaveChangesAsync(); } using (var ctx = new AuditPerTableContext()) { var order = await ctx.Order.SingleAsync(a => a.Number.Equals(id)); var orderlineAudits = ctx.OrderlineAudit.AsNoTracking().Where(a => a.OrderId.Equals(order.Id)).ToList(); Assert.AreEqual(0, orderlineAudits.Count); } }
public async Task Test_EFDataProvider_Async() { var dp = new EntityFrameworkDataProvider(); dp.AuditTypeMapper = t => { if (t == typeof(Order)) { return(typeof(OrderAudit)); } if (t == typeof(Orderline)) { return(typeof(OrderlineAudit)); } return(null); }; dp.AuditEntityAction = (ev, entry, obj) => { var ab = obj as AuditBase; if (ab != null) { ab.AuditDate = DateTime.UtcNow; ab.UserName = ev.Environment.UserName; ab.AuditStatus = entry.Action; } return(true); }; Audit.Core.Configuration.Setup() .UseCustomProvider(dp); var id = Guid.NewGuid().ToString(); using (var ctx = new AuditPerTableContext()) { var o = new Order() { Number = id, Status = "Pending", OrderLines = new Collection <Orderline>() { new Orderline() { Product = "p1: " + id, Quantity = 2 }, new Orderline() { Product = "p2: " + id, Quantity = 3 } } }; await ctx.AddAsync(o); await ctx.SaveChangesAsync(); } using (var ctx = new AuditPerTableContext()) { var orderAudit = await ctx.OrderAudit.AsNoTracking().SingleOrDefaultAsync(a => a.Number.Equals(id)); Assert.NotNull(orderAudit); var orderlineAudits = await ctx.OrderlineAudit.AsNoTracking().Where(a => a.OrderId.Equals(orderAudit.Id)).ToListAsync(); Assert.AreEqual(2, orderlineAudits.Count); Assert.AreEqual("p1: " + id, orderlineAudits[0].Product); Assert.AreEqual("p2: " + id, orderlineAudits[1].Product); Assert.AreEqual("Insert", orderAudit.AuditStatus); Assert.AreEqual("Insert", orderlineAudits[0].AuditStatus); Assert.AreEqual("Insert", orderlineAudits[1].AuditStatus); Assert.AreEqual(orderlineAudits[0].UserName, orderlineAudits[1].UserName); Assert.AreEqual(orderlineAudits[0].UserName, orderAudit.UserName); Assert.IsFalse(string.IsNullOrWhiteSpace(orderlineAudits[0].UserName)); Assert.IsFalse(string.IsNullOrWhiteSpace(orderlineAudits[1].UserName)); } using (var ctx = new AuditPerTableContext()) { var o = ctx.Order.Single(a => a.Number.Equals(id)); o.Status = "Cancelled"; await ctx.SaveChangesAsync(); } using (var ctx = new AuditPerTableContext()) { var orderAudits = await ctx.OrderAudit.AsNoTracking().Where(a => a.Number.Equals(id)).OrderByDescending(a => a.AuditDate).ToListAsync(); Assert.AreEqual(2, orderAudits.Count); Assert.AreEqual("Update", orderAudits[0].AuditStatus); Assert.AreEqual("Cancelled", orderAudits[0].Status); Assert.AreEqual("Pending", orderAudits[1].Status); Assert.AreEqual("Insert", orderAudits[1].AuditStatus); } using (var ctx = new AuditPerTableContext()) { var order = await ctx.Order.SingleAsync(a => a.Number.Equals(id)); var ol = await ctx.Orderline.SingleAsync(a => a.OrderId.Equals(order.Id) && a.Product.StartsWith("p1")); ctx.Remove(ol); await ctx.SaveChangesAsync(); } using (var ctx = new AuditPerTableContext()) { var order = await ctx.Order.SingleAsync(a => a.Number.Equals(id)); var orderlineAudits = await ctx.OrderlineAudit.AsNoTracking().Where(a => a.OrderId.Equals(order.Id)).OrderByDescending(a => a.AuditDate).ToListAsync(); Assert.AreEqual(3, orderlineAudits.Count); Assert.AreEqual("Delete", orderlineAudits[0].AuditStatus); Assert.IsTrue(orderlineAudits[0].Product.StartsWith("p1")); } }