private IEnumerable <AuditEntry> OnBeforeSaveChanges() { ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries().ToList()) { if (entry.Entity is EntityAudit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } if (_userLoggedRepository != null) { var userEntry = new EntityAuditUser() { Id = _userLoggedRepository.GetUserId(), Name = _userLoggedRepository.GetUserName(), Email = _userLoggedRepository.GetUserEmail() }; var auditEntry = new AuditEntry(entry) { EntityName = entry.Metadata.GetTableName(), TransactionId = Transaction.Current? .TransactionInformation?.LocalIdentifier ?? Guid.NewGuid().ToString(), User = userEntry, Operation = entry.State.ToOperationType() }; auditEntries.Add(auditEntry); foreach (var property in entry.Properties.ToList()) { var propertyName = property.Metadata.Name; if (property.IsTemporary) { auditEntry.TemporaryProperties.Add(property); continue; } if (property.Metadata.IsPrimaryKey()) { auditEntry.SetKeyValues(propertyName, property.CurrentValue); continue; } switch (entry.State) { case EntityState.Added: auditEntry.SetNewValues(propertyName, property.CurrentValue); break; case EntityState.Deleted: auditEntry.SetOldValues(propertyName, property.CurrentValue); break; case EntityState.Modified: if (property.IsModified && !Nullable.Equals(property.OriginalValue, property.CurrentValue)) { auditEntry.SetOldValues(propertyName, property.OriginalValue); auditEntry.SetNewValues(propertyName, property.CurrentValue); } break; } } } foreach (var auditEntry in auditEntries.Where(e => !e.HasTemporaryProperties)) { EntityAudits.Add(auditEntry); } } return(auditEntries .Where(e => e.HasTemporaryProperties).ToList()); }