private void OnBeforeSaveChanges(string userId) { ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries()) { if (entry.Entity is Audit || entry.State is EntityState.Detached || entry.State is EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry) { TableName = entry.Entity.GetType().Name, UserId = userId }; auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { string propertyName = property.Metadata.Name; if (property.Metadata.IsPrimaryKey()) { auditEntry.KeyValues[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.AuditType = Enums.AuditType.Create; auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.AuditType = Enums.AuditType.Delete; auditEntry.NewValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: if (property.IsModified) { auditEntry.ChangedColumns.Add(propertyName); auditEntry.AuditType = Enums.AuditType.Update; auditEntry.OldValues[propertyName] = property.OriginalValue; auditEntry.NewValues[propertyName] = property.CurrentValue; } break; default: _ = entry.State; break; } } } foreach (var auditEntry in auditEntries) { AuditLogs.Add(auditEntry.ToAudit()); } }
private List <AuditEntry> OnBeforeSaveChanges() { ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries()) { if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged || entry.State == EntityState.Added) { continue; } var auditEntry = new AuditEntry(entry); auditEntry.TableName = entry.Metadata.Relational().TableName; 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) { case EntityState.Deleted: auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: 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)) { Set <Audit>().Add(auditEntry.ToAudit()); } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList());; }
public async Task AuditLogging() { ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries()) { if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry) { TableName = entry.Entity.GetType().Name, UserId = _currentUser.UserId, UserName = _currentUser.UserName }; auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { var propertyName = property.Metadata.Name; if (property.Metadata.IsPrimaryKey()) { auditEntry.KeyValues[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.AuditType = AuditType.Create; auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.AuditType = AuditType.Delete; auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: if (property.IsModified) { auditEntry.ChangedColumns.Add(propertyName); auditEntry.AuditType = AuditType.Update; auditEntry.OldValues[propertyName] = property.OriginalValue; auditEntry.NewValues[propertyName] = property.CurrentValue; } break; } } } foreach (var auditEntry in auditEntries) { await Audits.AddAsync(auditEntry.ToAudit()); } }
private void OnBeforeSaveChanges(string userId) { ChangeTracker.DetectChanges(); //Scans the entities for any changes. var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries()) { if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry); auditEntry.TableName = entry.Entity.GetType().Name; //Get the Table Name from the entity object. auditEntry.UserId = userId; auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { string propertyName = property.Metadata.Name; //If the current property is a primary key, then add it to the PrimaryKey Dictionary and skip it. if (property.Metadata.IsPrimaryKey()) { auditEntry.KeyValues[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.AuditType = AuditType.Create; auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.AuditType = AuditType.Delete; auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: if (property.IsModified) { auditEntry.ChangedColumns.Add(propertyName); auditEntry.AuditType = AuditType.Update; auditEntry.OldValues[propertyName] = property.OriginalValue; auditEntry.NewValues[propertyName] = property.CurrentValue; } break; } } } foreach (var auditEntry in auditEntries) { AuditLogs.Add(auditEntry.ToAudit()); } }
private List <AuditEntry> OnBeforeSaveChanges() { ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries()) { if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry); auditEntry.TableName = entry.Metadata.GetTableName(); auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { // The following condition is ok with EF Core 2.2 onwards. // If you are using EF Core 2.1, you may need to change the following condition to support navigation properties: https://github.com/dotnet/efcore/issues/17700 if (property.IsTemporary || (entry.State == EntityState.Added && property.Metadata.IsForeignKey())) { // 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) { case EntityState.Added: auditEntry.NewValues[propertyName] = Convert.ToString(property.CurrentValue); break; case EntityState.Deleted: auditEntry.OldValues[propertyName] = Convert.ToString(property.OriginalValue); break; case EntityState.Modified: if (property.IsModified) { auditEntry.OldValues[propertyName] = Convert.ToString(property.OriginalValue); auditEntry.NewValues[propertyName] = Convert.ToString(property.CurrentValue); } break; } } } // Save audit entities that have all the modifications foreach (var auditEntry in auditEntries.Where(a => !a.HasTemporaryProperties)) { Audits.Add(auditEntry.ToAudit()); } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(a => a.HasTemporaryProperties).ToList()); }
List <AuditEntry> beforeSaveChanges(string userName, DateTimeOffset now) { List <AuditEntry> auditEntries = new List <AuditEntry>(); foreach (EntityEntry entry in ChangeTracker.Entries()) { if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } AuditEntry auditEntry = new AuditEntry(entry); auditEntry.TableName = entry.Metadata.GetTableName(); //entry.Metadata.Relational().TableName; auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { if (property.IsTemporary) { auditEntry.TemporaryProperties.Add(property); // value will be generated by the database, get the value after saving continue; } string propertyName = property.Metadata.Name; if (property.Metadata.IsPrimaryKey()) { auditEntry.KeyValues[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.NewValues[propertyName] = property.CurrentValue; auditEntry.ChangeType = ChangeTypes.Create; break; case EntityState.Deleted: auditEntry.OldValues[propertyName] = property.OriginalValue; auditEntry.ChangeType = ChangeTypes.Delete; break; case EntityState.Modified: auditEntry.ChangeType = ChangeTypes.Update; if (property.IsModified) { auditEntry.OldValues[propertyName] = property.OriginalValue; auditEntry.NewValues[propertyName] = property.CurrentValue; } break; } } } foreach (AuditEntry auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties)) { Audits.Add(auditEntry.ToAudit(userName, now)); // Save audit entities that have all the modifications } return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList()); // keep a list of entries where the value of some properties are unknown at this step }
private List <AuditEntry> OnBeforeSaveChanges(string userId) { ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries()) { if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry); auditEntry.TableName = entry.Entity.GetType().Name; auditEntry.UserId = userId; auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { if (property.IsTemporary) { auditEntry.TemporaryProperties.Add(property); continue; } string propertyName = property.Metadata.Name; if (property.Metadata.IsPrimaryKey()) { auditEntry.KeyValues[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.AuditType = AuditType.Create; auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.AuditType = AuditType.Delete; auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: if (property.IsModified) { auditEntry.ChangedColumns.Add(propertyName); auditEntry.AuditType = AuditType.Update; auditEntry.OldValues[propertyName] = property.OriginalValue; auditEntry.NewValues[propertyName] = property.CurrentValue; } break; } } } foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties)) { AuditTrails.Add(auditEntry.ToAudit()); } return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList()); }
private async Task <List <AuditEntry> > OnBeforeSaveChanges(string userId) { ChangeTracker.DetectChanges(); var markedAsDeleted = ChangeTracker.Entries().Where(e => e.State == EntityState.Deleted); SoftDeleteEntities(markedAsDeleted); var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries()) { if (entry.Entity is AuditEntity || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry) { 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; } var propertyName = property.Metadata.Name; if (property.Metadata.IsPrimaryKey()) { auditEntry.KeyValues[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: if (property.OriginalValue == null && property.CurrentValue == null) { continue; } if (property.OriginalValue == null || property.CurrentValue == null || !property.OriginalValue.Equals(property.CurrentValue)) { entry.OriginalValues.SetValues(await entry.GetDatabaseValuesAsync()); 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)) { auditEntry.UserId = userId; await Set <AuditEntity>().AddAsync(auditEntry.ToAudit()); } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList()); }
public static List <AuditEntry> AuditBeforeSaveChanges(this DbContext context) { var auditEntries = new List <AuditEntry>(); if (context.GetModelTypes().Contains(typeof(Audit))) { context.ChangeTracker.DetectChanges(); foreach (var entry in context.ChangeTracker.Entries()) { if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry); auditEntry.TableName = entry.Metadata.GetTableName(); //.NET Core 2.2 //auditEntry.TableName = entry.Metadata.Relational().TableName; auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { if (EntityAuditableType.GetProperty(property.Metadata.Name) != null) { continue; } 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) { case EntityState.Added: auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: if (property.IsModified) { if (property.OriginalValue == null && property.CurrentValue == null) { continue; } if (property.OriginalValue == null || property.CurrentValue == null || !property.OriginalValue.Equals(property.CurrentValue)) { 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)) { context.Add(auditEntry.ToAudit()); } } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList()); }
private List <AuditEntry> OnBeforeSaveChanges() { _context.ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in _context.ChangeTracker.Entries()) { // if is an audit log or is an AccessLog or is detched or is unchanged if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged || entry.Entity is AccessLogEntity || entry.Entity is UpdateState) { continue; } var auditEntry = new AuditEntry(entry) { ObjectType = entry.Metadata.Name }; auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { if (property.Metadata.ClrType != typeof(UpdateState) || property.Metadata.Name == "LastViewed") { continue; } string propertyName = property.Metadata.Name; if (property.IsTemporary) { // value will be generated by the database, get the value after saving auditEntry.TemporaryProperties.Add(property); continue; } if (property.Metadata.IsPrimaryKey()) { auditEntry.KeyValues[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: if (property.IsModified) { auditEntry.OldValues[propertyName] = property.OriginalValue; auditEntry.NewValues[propertyName] = property.CurrentValue; } break; } } } // Ensure we don't add have any blank audit entries auditEntries = auditEntries.Where(ae => ae.PropertyChangedCount != 0).ToList(); // Save audit entities that have all the modifications foreach (var auditEntry in auditEntries.Where(e => !e.HasTemporaryProperties)) { _context.AuditItems.Add(auditEntry.ToAudit()); } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(e => e.HasTemporaryProperties).ToList()); }
private List <AuditEntry> OnBeforeSaveChanges(long currentHouseId, long currentUserId, long?targetObjectId, long objectId) { context.ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in context.ChangeTracker.Entries()) { if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry); auditEntry.TableName = entry.Metadata.GetTableName(); auditEntry.HouseId = currentHouseId; auditEntry.UserId = currentUserId; auditEntry.TargetObjectId = targetObjectId; auditEntry.ObjectId = objectId; 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) { case EntityState.Added: auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: if (property.IsModified) { var latestAuditValues = context.Audits.OrderByDescending(x => x.Id).Where(x => x.ObjectId == objectId).FirstOrDefault().NewValues; var auditProperties = JsonConvert.DeserializeObject <Dictionary <string, string> >(latestAuditValues); auditEntry.OldValues[propertyName] = auditProperties[propertyName]; auditEntry.NewValues[propertyName] = property.CurrentValue; } break; } } } // Save audit entities that have all the modifications foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties)) { context.Audits.Add(auditEntry.ToAudit()); } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList()); }
public partial List <AuditEntry> OnBeforeSaveChanges() { ChangeTracker.DetectChanges(); var date = DateTime.Now; var userId = _sessionUser.GetId(); var auditEntries = new List <AuditEntry>(); var sequentialId = new SequentialGuidValueGenerator(); foreach (var entry in ChangeTracker.Entries().Where(e => e.Entity is BaseEntity)) { if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry); auditEntry.EntityName = entry.Entity.GetType().Name; auditEntry.UserId = userId; auditEntries.Add(auditEntry); switch (entry.State) { case EntityState.Added: var guid = sequentialId.Next(null); ((BaseEntity)entry.Entity).RowId = guid; ((BaseEntity)entry.Entity).Referral = string.IsNullOrEmpty(((BaseEntity)entry.Entity).Referral) ? guid.ToString().Replace("-", "").ToUpper().Substring(0, 20) : ((BaseEntity)entry.Entity).Referral; ((BaseEntity)entry.Entity).CreatedUser = userId; ((BaseEntity)entry.Entity).CreatedOn = DateTime.Now; auditEntry.Action = Enums.Audit.Action.Created; break; case EntityState.Deleted: entry.State = EntityState.Modified; ((BaseEntity)entry.Entity).IsDeleted = true; auditEntry.Action = Enums.Audit.Action.Deleted; //_logger.LogInformation($"Data Context: \"{entry.Entity.GetType().Name}\" deleted. Writing to Audits"); break; case EntityState.Modified: ((BaseEntity)entry.Entity).IsModified = true; auditEntry.Action = Enums.Audit.Action.Modified; //_logger.LogInformation($"Data Context: \"{entry.Entity.GetType().Name}\" modified. Writing to Audits"); break; } foreach (var property in entry.Properties) { if (property.IsTemporary || property.Metadata.Name.Contains("RowId") || property.Metadata.Name.Contains("RowVersion")) { // 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.PrimaryKeys[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: //if (property.IsModified) //{ if (auditEntry.Action != Enums.Audit.Action.Deleted) { if (property.IsModified) { auditEntry.NewValues[propertyName] = property.CurrentValue; } } else { if (property.Metadata.Name.Contains("IsDeleted")) { auditEntry.NewValues[propertyName] = property.CurrentValue; } } auditEntry.OldValues[propertyName] = property.OriginalValue; //} break; } } } // Save audit entities that have all the modifications foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties)) { Audits.Add(auditEntry.ToAudit()); } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList()); }
private List <AuditEntry> OnBeforeSaveChanges() { _context.ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in _context.ChangeTracker.Entries()) { var attributeValue = entry.Metadata.ClrType.CustomAttribute <AuditTrailsAttribute>(); if (attributeValue == null || attributeValue.Ignore) { continue; } var currentValues = entry.GetDatabaseValues(); if (currentValues != null) { entry.OriginalValues.SetValues(currentValues); } var auditEntry = new AuditEntry(entry) { TableName = entry.Metadata.Relational().TableName }; auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { if (property?.OriginalValue == null && property?.CurrentValue != null) { property.IsModified = false; } if (property.IsModified && property?.CurrentValue == null && property?.OriginalValue != null) { property.IsModified = false; } if (property.IsModified && property?.CurrentValue == null && property?.OriginalValue == null) { property.IsModified = false; } if (property.IsModified && property.OriginalValue.Equals(property.CurrentValue)) { property.IsModified = false; } if (property.Metadata.PropertyInfo == null) { continue; } var propertyAuditAttribute = property.Metadata.PropertyInfo.PropertyCustomAttribute <AuditTrailsAttribute>(); if (propertyAuditAttribute != null && propertyAuditAttribute.Ignore) { continue; } if (property.IsTemporary) { // value will be generated by the database, get the value after saving auditEntry.TemporaryProperties.Add(property); continue; } //property.Metadata.PropertyInfo.GetCustomAttributes() var propertyName = property.Metadata.Name; if (property.Metadata.IsPrimaryKey()) { auditEntry.KeyValues[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.NewValues[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.OldValues[propertyName] = property.OriginalValue; break; case EntityState.Modified: 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)) { if (auditEntry.NewValues.Count > 0 || auditEntry.OldValues.Count > 0) { Audits.Add(auditEntry.ToAudit()); } } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList()); }
private List <AuditEntry> OnBeforeSaveChanges() { ChangeTracker.DetectChanges(); var auditEntries = new List <AuditEntry>(); foreach (var entry in ChangeTracker.Entries()) { if (entry.Entity is Auditoria || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged) { continue; } var auditEntry = new AuditEntry(entry) { Tabela = entry.Metadata.Relational().TableName.ToUpper(), Usuario = _variables.UserName }; auditEntries.Add(auditEntry); foreach (var property in entry.Properties) { if (property.IsTemporary) { auditEntry.TemporaryProperties.Add(property); continue; } string propertyName = property.Metadata.Name; if (property.Metadata.IsPrimaryKey()) { auditEntry.IdsAlterados[propertyName] = property.CurrentValue; continue; } switch (entry.State) { case EntityState.Added: auditEntry.ValoresNovos[propertyName] = property.CurrentValue; break; case EntityState.Deleted: auditEntry.ValoresAntigos[propertyName] = property.OriginalValue; break; case EntityState.Modified: if (property.IsModified) { if ((property.CurrentValue == null && property.OriginalValue != null) || !property.CurrentValue.Equals(property.OriginalValue)) { auditEntry.ValoresAntigos[propertyName] = property.OriginalValue; auditEntry.ValoresNovos[propertyName] = property.CurrentValue; } } break; } } } // Save audit entities that have all the modifications foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties)) { _auditoriaRepository.AddLog(auditEntry.ToAudit()).Wait(); } // keep a list of entries where the value of some properties are unknown at this step return(auditEntries.Where(_ => _.HasTemporaryProperties).ToList()); }