public static List <PropertyChangeDescription> For(DbEntityEntry dbEntry, TrackingEntityInfo config) { var propertyChanges = new List <PropertyChangeDescription>(); switch (dbEntry.State) { case EntityState.Added: { propertyChanges = GetChangesFor(null, dbEntry.Entity, config); break; } case EntityState.Modified: { var originalEntity = GetOriginalEntity(dbEntry); propertyChanges = GetChangesFor(originalEntity, dbEntry.Entity, config); break; } case EntityState.Deleted: { var originalEntity = GetOriginalEntity(dbEntry); propertyChanges = GetChangesFor(originalEntity, null, config); break; } } return(propertyChanges); }
public static TrackEntityChange GetTrackEntityChangeRecord( DataContext dataContext, DbEntityEntry dbEntry, TrackingEntityInfo trackingEntityConfig) { var supportedEntryState = new List <EntityState> { EntityState.Modified, EntityState.Added, EntityState.Deleted }; if (!supportedEntryState.Contains(dbEntry.State)) { return(null); } var changedDateUtc = DateTime.UtcNow; if (dbEntry.Entity is BaseEntity baseEntity) { changedDateUtc = baseEntity.UpdatedDateUtc; } var propertyChanges1 = new List <PropertyChangeDescription>(); var way1ExecutionTime = CalcExecutionTime.For(() => { propertyChanges1 = GetPropertyChangesWay1.For(dbEntry, trackingEntityConfig); }); var propertyChanges2 = new List <PropertyChangeDescription>(); var way2ExecutionTime = CalcExecutionTime.For(() => { propertyChanges2 = GetPropertyChangesWay2.For(dbEntry, trackingEntityConfig); }); object oldEntity = null; var oldEntityGettingExecutionTime = CalcExecutionTime.For(() => { oldEntity = dbEntry.State == EntityState.Added ? null : GetPropertyChangesWay2.GetOriginalEntity(dbEntry); }); var trackEntityChange = new TrackEntityChange { Id = Guid.NewGuid(), EntityTable = trackingEntityConfig.EntityName, EntityId = GetPrimaryKeyId(dataContext, dbEntry), ChangeType = dbEntry.State.ToString(), ChangeDateUtc = changedDateUtc, EntityBeforeChangeSnapshot = oldEntity?.ToJson(), TimeOfGetOldEntity = oldEntityGettingExecutionTime.TotalMilliseconds, EntityAfterChangeSnapshot = dbEntry.State != EntityState.Deleted ? dbEntry.Entity.ToJson() : null, PropertiesChangesWay1 = propertyChanges1.ToJson(), TimeOfWay1 = way1ExecutionTime.TotalMilliseconds, PropertiesChangesWay2 = propertyChanges2.ToJson(), TimeOfWay2 = way2ExecutionTime.TotalMilliseconds, ChangedByUserId = UserManager.GetCurrentUserId(), }; return(trackEntityChange); }
public static List <PropertyChangeDescription> For( DbEntityEntry dbEntry, TrackingEntityInfo trackingEntityConfig) { var trackPropertiesWithName = trackingEntityConfig.PropertyList.Select(x => x.Name).ToList(); var propertyChanges = new List <PropertyChangeDescription>(); switch (dbEntry.State) { case EntityState.Added: { propertyChanges = ( from propertyName in dbEntry.CurrentValues.PropertyNames where trackPropertiesWithName.Contains(propertyName) select new PropertyChangeDescription { PropertyName = propertyName, OldValue = null, NewValue = dbEntry.CurrentValues.GetValue <object>(propertyName) == null ? null : dbEntry.CurrentValues.GetValue <object>(propertyName).ToString() }) .ToList(); break; } case EntityState.Modified: { propertyChanges = ( from propertyName in dbEntry.CurrentValues.PropertyNames where trackPropertiesWithName.Contains(propertyName) where !object.Equals(dbEntry.OriginalValues.GetValue <object>(propertyName), dbEntry.CurrentValues.GetValue <object>(propertyName)) select new PropertyChangeDescription { PropertyName = propertyName, OldValue = dbEntry.OriginalValues.GetValue <object>(propertyName) == null ? null : dbEntry.OriginalValues.GetValue <object>(propertyName).ToString(), NewValue = dbEntry.CurrentValues.GetValue <object>(propertyName) == null ? null : dbEntry.CurrentValues.GetValue <object>(propertyName).ToString() }) .ToList(); break; } case EntityState.Deleted: { propertyChanges = ( from propertyName in dbEntry.OriginalValues.PropertyNames where trackPropertiesWithName.Contains(propertyName) select new PropertyChangeDescription { PropertyName = propertyName, OldValue = dbEntry.OriginalValues.GetValue <object>(propertyName) == null ? null : dbEntry.OriginalValues.GetValue <object>(propertyName).ToString(), NewValue = null }) .ToList(); break; } } return(propertyChanges.ToList()); }
public static List <PropertyChangeDescription> GetChangesFor <T>(T oldEntity, T newEntity, TrackingEntityInfo propertyConfig) where T : class { var changeList = new List <PropertyChangeDescription>(); var propertyList = typeof(T).GetProperties(); foreach (var property in propertyList) { if (propertyConfig.PropertyList.Select(x => x.Name).Contains(property.Name) == false) { continue; } object oldValue = null; if (oldEntity != null) { oldValue = property.GetValue(oldEntity); } object newValue = null; if (newEntity != null) { newValue = property.GetValue(newEntity); } if (oldValue?.ToString() == newValue?.ToString()) { continue; } var change = new PropertyChangeDescription { PropertyName = property.Name, OldValue = oldValue?.ToString(), NewValue = newValue?.ToString(), }; changeList.Add(change); } return(changeList); }