private static Audit ToAuditItem(this AuditEntry item, string transactionId,
                                  JsonSerializerSettings jsonSettings)
 {
     return(new Audit
     {
         CreatedBy = item.CreateBy,
         CreateDate = item.CreateDate,
         Client = item.Client,
         Action = item.Action,
         TableName = item.TableName,
         RowId = JsonConvert.SerializeObject(item.KeyValues, jsonSettings),
         Data = JsonConvert.SerializeObject(item.Changes, jsonSettings),
         TransactionId = transactionId
     });
 }
        private static List <AuditEntry> OnBeforeSaveChanges(this DbContext context, AuditOptions options)
        {
            context.ChangeTracker.DetectChanges();
            var auditEntries = new List <AuditEntry>();

            foreach (var entry in context.ChangeTracker.Entries())
            {
                if (entry.State == EntityState.Detached || entry.State == EntityState.Unchanged)
                {
                    continue;
                }

                var auditEntry = new AuditEntry(entry)
                {
                    TableName  = entry.Metadata.GetTableName(),
                    CreateBy   = options.User(),
                    Client     = options.Client(),
                    CreateDate = options.CurrentDateTime(),
                    Action     = entry.State.ToString()
                };
                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.GetColumnName();
                    if (property.Metadata.IsPrimaryKey())
                    {
                        auditEntry.KeyValues[propertyName] = property.CurrentValue;
                        continue;
                    }

                    switch (entry.State)
                    {
                    case EntityState.Added:
                        auditEntry.Changes[propertyName] = new AuditEntry.Change
                        {
                            NewValue = property.CurrentValue
                        };
                        break;

                    case EntityState.Deleted:
                        auditEntry.Changes[propertyName] = new AuditEntry.Change
                        {
                            OldValue = property.OriginalValue
                        };
                        break;

                    case EntityState.Modified:
                        if (property.IsModified)
                        {
                            auditEntry.Changes[propertyName] = new AuditEntry.Change
                            {
                                OldValue = property.OriginalValue,
                                NewValue = property.CurrentValue
                            }
                        }
                        ;

                        break;
                    }
                }
            }

            return(auditEntries.ToList());
        }