Пример #1
0
        private void _TrackUpdated(DateTime now)
        {
            var entities = ChangeTracker.Entries().Where(p => p.State == EntityState.Modified).ToList();

            foreach (var entity in entities)
            {
                var primaryKey     = GetPrimaryKeyValue(entity);
                var entityName     = entity.Entity.GetType().Name;
                var databaseValues = entity.GetDatabaseValues();

                foreach (var propName in entity.OriginalValues.PropertyNames)
                {
                    if (propName == "DateCreated" || propName == "LastUpdated")
                    {
                        continue;
                    }

                    var currentValue  = getStringValue(entity.CurrentValues[propName]);
                    var originalValue = getStringValue(databaseValues.GetValue <object>(propName));

                    if (originalValue != currentValue)
                    {
                        var auditRecord = new AuditRecord()
                        {
                            EntityName      = entityName,
                            PrimaryKeyValue = primaryKey.ToString(),
                            Action          = "Updated",
                            PropertyName    = propName,
                            OldValue        = originalValue,
                            NewValue        = currentValue,
                            DateChanged     = now,
                            UserName        = this.UserName,
                            Operator        = this.Operator
                        };

                        AuditRecords.Add(auditRecord);
                    }
                }
            }
        }
Пример #2
0
        private void _TrackDeleted(DateTime now)
        {
            var entities = ChangeTracker.Entries().Where(p => p.State == EntityState.Deleted).ToList();

            foreach (var entity in entities)
            {
                var primaryKey = GetPrimaryKeyValue(entity);
                var entityName = entity.Entity.GetType().Name;

                var auditRecord = new AuditRecord()
                {
                    EntityName      = entityName,
                    PrimaryKeyValue = primaryKey.ToString(),
                    Action          = "Deleted",
                    DateChanged     = now,
                    UserName        = this.UserName,
                    Operator        = this.Operator
                };

                AuditRecords.Add(auditRecord);
            }
        }