コード例 #1
0
        private DBAudit AuditTrailFactory(ObjectStateEntry entry, string UserName)
        {
            DBAudit audit = new DBAudit();

            audit.AuditId       = Guid.NewGuid().ToString();
            audit.RevisionStamp = DateTime.Now;
            audit.TableName     = entry.EntitySet.Name;
            audit.UserName      = UserName;

            if (entry.State == EntityState.Added)
            {//entry is Added
                audit.NewData = GetEntryValueInString(entry, false);
                audit.Actions = AuditActions.I.ToString();
            }
            else if (entry.State == EntityState.Deleted)
            {//entry in deleted
                audit.OldData = GetEntryValueInString(entry, true);
                audit.Actions = AuditActions.D.ToString();
            }
            else
            {//entry is modified
                audit.OldData = GetEntryValueInString(entry, true);
                audit.NewData = GetEntryValueInString(entry, false);
                audit.Actions = AuditActions.U.ToString();

                IEnumerable <string> modifiedProperties = entry.GetModifiedProperties();
                //assing collection of mismatched Columns name as serialized string
                audit.ChangedColumns = XMLSerializationHelper.XmlSerialize(modifiedProperties.ToArray());
            }

            return(audit);
        }
コード例 #2
0
        void AdventureWorksEntities_SavingChanges(object sender, EventArgs e)
        {
            IEnumerable <ObjectStateEntry> changes = this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified);

            foreach (ObjectStateEntry stateEntryEntity in changes)
            {
                if (!stateEntryEntity.IsRelationship &&
                    stateEntryEntity.Entity != null &&
                    !(stateEntryEntity.Entity is DBAudit))
                {//is a normal entry, not a relationship
                    DBAudit audit = this.AuditTrailFactory(stateEntryEntity, UserName);
                    auditTrailList.Add(audit);
                }
            }

            if (auditTrailList.Count > 0)
            {
                foreach (var audit in auditTrailList)
                {//add all audits
                    this.AddToDBAudit(audit);
                }
            }
        }