Esempio n. 1
0
        /// <summary>
        /// Updates column values and primary keys on the Audit Event after the EF save operation completes.
        /// </summary>
        public void UpdateAuditEvent(EntityFrameworkEvent efEvent, IAuditDbContext context)
        {
            foreach (var efEntry in efEvent.Entries)
            {
                var entry      = efEntry.Entry;
                var entityName = new EntityName()
                {
                    Schema = efEntry.Schema, Table = efEntry.Table
                };
                efEntry.PrimaryKey = GetPrimaryKey(context.DbContext, entry, entityName);
                foreach (var pk in efEntry.PrimaryKey)
                {
                    if (efEntry.ColumnValues.ContainsKey(pk.Key))
                    {
                        efEntry.ColumnValues[pk.Key] = pk.Value;
                    }
                }
                var fks = GetForeignKeys(context.DbContext, entry, entityName);
                foreach (var fk in fks)
                {
#if EF_FULL
                    // When deleting an entity, sometimes the foreign keys are set to NULL by EF. This only happens on EF6.
                    if (fk.Value == null)
                    {
                        continue;
                    }
#endif
                    if (efEntry.ColumnValues.ContainsKey(fk.Key))
                    {
                        efEntry.ColumnValues[fk.Key] = fk.Value;
                    }
                }
            }
#if EF_FULL
            if (efEvent.Associations != null)
            {
                foreach (var association in efEvent.Associations)
                {
                    var e1 = association.Records[0].InternalEntity;
                    var e2 = association.Records[1].InternalEntity;
                    association.Records[0].PrimaryKey = EntityKeyHelper.Instance.GetPrimaryKeyValues(e1, context.DbContext);
                    association.Records[1].PrimaryKey = EntityKeyHelper.Instance.GetPrimaryKeyValues(e2, context.DbContext);
                }
            }
#endif
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the entities changes for this entry.
        /// </summary>
        /// <param name="context">The audit db context.</param>
        /// <param name="entry">The entry.</param>
        /// <param name="entityName">The entity name.</param>
        private List <EventEntryChange> GetChanges(IAuditDbContext context, EntityEntry entry, EntityName entityName)
        {
            var result = new List <EventEntryChange>();
            var props  = entry.Metadata.GetProperties();

            foreach (var prop in props)
            {
                PropertyEntry propEntry = entry.Property(prop.Name);
                if (propEntry.IsModified)
                {
                    if (IncludeProperty(context, entry, prop.Name))
                    {
                        result.Add(new EventEntryChange()
                        {
                            ColumnName    = GetColumnName(prop, entityName),
                            NewValue      = HasPropertyValue(context, entry, prop.Name, propEntry.CurrentValue, out object currValue) ? currValue : propEntry.CurrentValue,
                            OriginalValue = HasPropertyValue(context, entry, prop.Name, propEntry.OriginalValue, out object origValue) ? origValue : propEntry.OriginalValue
                        });