Example #1
0
        private AuditEntryInputDto CreateAuditEntry(EntityEntry entityEntry)
        {
            var entity      = entityEntry.Entity;
            var type        = entity.GetType();
            var displayName = type.ToDescription(); //得到实体上特性
            DataOperationType changeType = DataOperationType.Add;

            switch (entityEntry.State)
            {
            case EntityState.Deleted:
                changeType = DataOperationType.Delete;
                break;

            case EntityState.Modified:
                changeType = DataOperationType.Update;
                break;

            case EntityState.Added:
                changeType = DataOperationType.Add;
                break;
            }
            AuditEntryInputDto auditEntryInput = new AuditEntryInputDto();

            auditEntryInput.KeyValues              = new Dictionary <string, object>();
            auditEntryInput.EntityAllName          = type.FullName;
            auditEntryInput.EntityDisplayName      = displayName;
            auditEntryInput.OperationType          = changeType;
            auditEntryInput.PropertysEntryInputDto = GetAuditPropertys(entityEntry);
            auditEntryInput.KeyValues              = new Dictionary <string, object>()
            {
                { "Id", GetEntityKey(entity) }
            };
            return(auditEntryInput);
        }
Example #2
0
        /// <summary>
        /// 获取实体状态记录审计日志
        /// </summary>
        /// <param name="Entries"></param>
        /// <returns></returns>
        public async Task <List <AuditEntryInputDto> > GetChangeTrackerList(IEnumerable <EntityEntry> Entries)
        {
            await Task.CompletedTask;
            var list = new List <AuditEntryInputDto>();

            foreach (var entityEntry in Entries)
            {
                var auditentry = new AuditEntryInputDto();
                auditentry.TableName         = entityEntry.Metadata.GetTableName();
                auditentry.EntityAllName     = entityEntry.Metadata.Name;
                auditentry.EntityDisplayName = entityEntry.Entity.GetType().ToDescription();
                //auditentry.TableName=
                switch (entityEntry.State)
                {
                case EntityState.Detached:
                    auditentry.OperationType = DataOperationType.None;
                    break;

                case EntityState.Unchanged:
                    auditentry.OperationType = DataOperationType.None;
                    break;

                case EntityState.Deleted:
                    auditentry.OperationType = DataOperationType.Delete;
                    break;

                case EntityState.Modified:
                    auditentry.OperationType = DataOperationType.Update;
                    break;

                case EntityState.Added:
                    auditentry.OperationType = DataOperationType.Add;
                    break;
                }
                var properties = entityEntry.Metadata.GetProperties();
                foreach (var propertie in properties)
                {
                    var AuditPropertys = new AuditPropertysEntryInputDto();
                    var propertyEntry  = entityEntry.Property(propertie.Name);//获取字段名
                    if (propertyEntry.Metadata.IsPrimaryKey())
                    {
                        auditentry.KeyValues.Add(propertie.Name, propertyEntry.CurrentValue?.ToString());
                    }
                    else
                    {
                        AuditPropertys.Properties           = propertie.Name;
                        AuditPropertys.NewValues            = propertyEntry.CurrentValue?.ToString();
                        AuditPropertys.OriginalValues       = propertyEntry.OriginalValue?.ToString();
                        AuditPropertys.PropertiesType       = propertie.ClrType.FullName;
                        AuditPropertys.PropertieDisplayName = propertyEntry.Metadata.PropertyInfo.ToDescription();
                        auditentry.PropertysEntryInputDto.Add(AuditPropertys);
                    }
                }
                list.Add(auditentry);
            }
            return(list);
        }