Esempio n. 1
0
        public async Task AllTest()
        {
            var entity = new DataTabulation <string, string>();

            var createdBy        = entity.CreatedBy;
            var createdTime      = entity.CreatedTime;
            var createdTimeTicks = entity.CreatedTimeTicks;

            Thread.Sleep(1000);

            await entity.PopulateCreationAsync(new TestClockService(),
                                               StoreHelper.CreatedByTypeName <StoreHelperTests>()).ConfigureAwait();

            Assert.NotEqual(createdBy, entity.CreatedBy);
            Assert.NotEqual(createdTime, entity.CreatedTime);
            Assert.NotEqual(createdTimeTicks, entity.CreatedTimeTicks);
        }
        protected virtual (TAudit audit, List <TAuditProperty> auditProperties) CreateAuditAndProperties
            (IModel model, EntityEntry entry, string entityTableName, string entityId, int state)
        {
            var audit = CreateAudit();

            audit.TableName = entityTableName;
            audit.EntityId  = entityId;
            audit.State     = state;

            audit.Id                = DataGenerator.GenerateAuditId();
            audit.EntityTypeName    = StoreHelper.CreatedByTypeName(entry.Metadata.ClrType);
            audit.StateName         = entry.State.ToString();
            audit.PropertyTableName = GetAuditPropertyTableName(model);

            var auditProperties = new List <TAuditProperty>();

            foreach (var property in entry.CurrentValues.Properties)
            {
                if (property.IsConcurrencyToken || property.ClrType.IsDefined <NonAuditedAttribute>())
                {
                    continue;
                }

                var auditProperty = CreateAuditProperty();

                auditProperty.AuditId          = audit.Id;
                auditProperty.PropertyName     = property.Name;
                auditProperty.PropertyTypeName = StoreHelper.CreatedByTypeName(property.ClrType);

                switch (entry.State)
                {
                case EntityState.Added:
                    auditProperty.NewValue = entry.Property(property.Name).CurrentValue?.ToString();
                    break;

                case EntityState.Deleted:
                    auditProperty.OldValue = entry.Property(property.Name).OriginalValue?.ToString();
                    break;

                case EntityState.Modified:
                {
                    var currentValue  = entry.Property(property.Name).CurrentValue?.ToString();
                    var originalValue = entry.Property(property.Name).OriginalValue?.ToString();

                    if (currentValue != originalValue)
                    {
                        auditProperty.NewValue = currentValue;
                        auditProperty.OldValue = originalValue;
                    }
                }
                break;
                }

                auditProperties.Add(auditProperty);
            }

            PopulateAuditCreatedTime(entry, audit);
            PopulateAuditCreatedBy(entry, audit);

            return(audit, auditProperties);
        }