public void SaveChanges() { try { var auditFactory = new AuditTrailFactory(context); var entityList = context.ChangeTracker.Entries().Where(p => p.State == EntityState.Added || p.State == EntityState.Deleted || p.State == EntityState.Modified); IEnumerable <Core.Model.AuditTrail> audit = null; foreach (var entity in entityList) { audit = auditFactory.GetAudit(entity); } context.SaveChanges(); if (audit == null) { return; } foreach (var log in audit) { auditRepository.Add(log); } context.SaveChanges(); } catch (OptimisticConcurrencyException) { var refreshableObjects = context.ChangeTracker.Entries().Select(c => c.Entity).ToList(); ((IObjectContextAdapter)context).ObjectContext.Refresh(RefreshMode.ClientWins, refreshableObjects); } }
private void SaveWithAudit() { var auditFactory = new AuditTrailFactory(this); var entityList = ChangeTracker.Entries().Where(p => p.Entity != null && p.State != EntityState.Unchanged && (p.State == EntityState.Added || p.State == EntityState.Deleted || p.State == EntityState.Modified || (p.Entity is ChangeLog) == false)); entityList.ToList().ForEach(entity => { IEnumerable <ChangeLog> audits = auditFactory.GetAudits(entity); ChangeLog.AddRange(audits); }); }