示例#1
0
        private int SaveChanges(Func <int> saveChanges)
        {
            if (AuditDisabled)
            {
                return(saveChanges());
            }

            EntityFrameworkAuditEvent auditEvent = CreateAuditEvent();

            if (auditEvent == null)
            {
                return(saveChanges());
            }

            int result = saveChanges();                                                 // if the save changes throws an exception (fails), it will exit this function and we will not save the event

            foreach (DatabaseActionEntry auditEventEntry in auditEvent.DatabaseActions) // loop back over inserts to get new primary key value for database generated values
            {
                if (auditEventEntry.DatabaseAction == DatabaseAction.Insert)
                {
                    object      entity     = auditEventEntry.EntityEntry.Entity;
                    IEntityType entityType = Model.FindEntityType(entity.GetType());                     // TODO: cache
                    auditEventEntry.PrimaryKey = entityType.GetPrimaryKeyValue(entity);
                    foreach (KeyValuePair <string, object> keyValuePair in auditEventEntry.PrimaryKey)
                    {
                        auditEventEntry.ColumnValues[keyValuePair.Key].OriginalValue = keyValuePair.Value;
                    }
                }
            }

            ArgusOptions.StorageProvider.InsertEvent(auditEvent);

            return(result);
        }
示例#2
0
        private EntityFrameworkAuditEvent CreateAuditEvent()
        {
            List <EntityEntry> modifiedEntries = GetModifiedEntries();

            if (modifiedEntries.Count == 0)
            {
                return(null);
            }

            DbConnection connection   = this.IsProviderRelational() ? Database.GetDbConnection() : null;
            string       connectionID = connection?.GetConnectionID();

            EntityFrameworkAuditEvent auditEvent = new EntityFrameworkAuditEvent()
            {
                DatabaseActions = new List <DatabaseActionEntry>(),
            };

            if (Settings.IncludeDatabase)
            {
                auditEvent.Database = connection?.Database;
            }
            if (Settings.IncludeConnectionID)
            {
                auditEvent.ConnectionID = connectionID;
            }
            if (Settings.IncludeTransactionID)
            {
                auditEvent.TransactionID = this.GetCurrentTransactionID();
            }

            foreach (EntityEntry modifiedEntry in modifiedEntries)
            {
                object      entity     = modifiedEntry.Entity;
                IEntityType entityType = Model.FindEntityType(entity.GetType());                 // TODO: cache

                Dictionary <string, FieldValueChange> columnValues = modifiedEntry.State == EntityState.Modified ? GetChanges(modifiedEntry) : GetColumnValues(modifiedEntry);

                if (columnValues.Count == 0)
                {
                    continue;
                }

                DatabaseActionEntry entry = new DatabaseActionEntry()
                {
                    EntityEntry    = modifiedEntry,
                    DatabaseAction = EntityFrameworkHelpers.GetActionFromEntityState(modifiedEntry.State),
                    ColumnValues   = columnValues,
                    PrimaryKey     = entityType.GetPrimaryKeyValue(entity),
                    Table          = this.GetDbSetForType(entity.GetType()).Name            // TODO: give option of table name
                };

                auditEvent.DatabaseActions.Add(entry);
            }

            return(auditEvent.DatabaseActions.Count != 0 ? auditEvent : null);
        }