private List <AuditEntry> OnBeforeSaveChanges() { ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries()) { if (entry.Entity is Log || entry.Entity is AppUser || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry, _httpContextAccessor); auditEntry.TableName = entry.Metadata.GetTableName(); auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { if (property.IsTemporary) { // value will be generated by the database, get the value after saving auditEntry.TemporaryProperties.Add(property); continue; } string propertyName = property.Metadata.Name; //if (property.Metadata.IsPrimaryKey()) //{ // auditEntry.KeyValues[propertyName] = property.CurrentValue; // continue; //} switch (entry.State) { //Pegar a opertação case EntityState.Added: auditEntry.Operacao = "Adição"; auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.Operacao = "Exclusão"; auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: auditEntry.Operacao = "Atualização"; if (property.IsModified) { auditEntry.OldValues[propertyName] = property.OriginalValue; auditEntry.NewValues[propertyName] = property.CurrentValue; } break; } } } // Save audit entities that have all the modifications foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties)) { Logs.Add(auditEntry.ToLog()); } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList()); }