private EntityChange CreateEntityChange(EntityEntry entityEntry, bool shouldSaveEntityHistory) { var entity = entityEntry.Entity; EntityChangeType changeType; switch (entityEntry.State) { case EntityState.Added: changeType = EntityChangeType.Created; break; case EntityState.Deleted: changeType = EntityChangeType.Deleted; break; case EntityState.Modified: changeType = IsDeleted(entityEntry) ? EntityChangeType.Deleted : EntityChangeType.Updated; break; case EntityState.Detached: case EntityState.Unchanged: default: Logger.Error("Unexpected EntityState!"); return(null); } var entityId = GetEntityId(entity); if (entityId == null && changeType != EntityChangeType.Created) { Logger.Error("Unexpected null value for entityId!"); return(null); } var entityType = entity.GetType(); var entityChange = new EntityChange { ChangeType = changeType, EntityEntry = entityEntry, // [NotMapped] EntityId = entityId, EntityTypeFullName = entityType.FullName, PropertyChanges = GetPropertyChanges(entityEntry, shouldSaveEntityHistory), TenantId = AbpSession.TenantId }; if (!shouldSaveEntityHistory && entityChange.PropertyChanges.Count == 0) { return(null); } return(entityChange); }
private EntityChange CreateEntityChangeInfo(EntityEntry entityEntry) { var entity = entityEntry.Entity; EntityChangeType changeType; switch (entityEntry.State) { case EntityState.Added: changeType = EntityChangeType.Created; break; case EntityState.Deleted: changeType = EntityChangeType.Deleted; break; case EntityState.Modified: changeType = IsDeleted(entityEntry) ? EntityChangeType.Deleted : EntityChangeType.Updated; break; case EntityState.Detached: case EntityState.Unchanged: default: Logger.Error("Unexpected EntityState!"); return(null); } var entityId = GetEntityId(entity); if (entityId == null && changeType != EntityChangeType.Created) { Logger.Error("Unexpected null value for entityId!"); return(null); } var entityType = entity.GetType(); var entityChangeInfo = new EntityChange { ChangeType = changeType, EntityEntry = entityEntry, // [NotMapped] EntityId = entityId, EntityTypeFullName = entityType.FullName, PropertyChanges = GetPropertyChanges(entityEntry) }; return(entityChangeInfo); }
private EntityChange CreateEntityChange(DbEntityEntry entityEntry, EntityType entityType) { EntityChangeType changeType; switch (entityEntry.State) { case EntityState.Added: changeType = EntityChangeType.Created; break; case EntityState.Deleted: changeType = EntityChangeType.Deleted; break; case EntityState.Modified: changeType = entityEntry.IsDeleted() ? EntityChangeType.Deleted : EntityChangeType.Updated; break; case EntityState.Detached: case EntityState.Unchanged: default: Logger.Error("Unexpected EntityState!"); return(null); } var entityId = GetEntityId(entityEntry, entityType); if (entityId == null && changeType != EntityChangeType.Created) { Logger.Error("Unexpected null value for entityId!"); return(null); } var entityChange = new EntityChange { ChangeType = changeType, EntityEntry = entityEntry, // [NotMapped] EntityId = entityId, EntityTypeFullName = entityEntry.GetEntityBaseType().FullName, TenantId = AbpSession.TenantId }; return(entityChange); }
private DateTime GetChangeTime(EntityChange entityChange) { var entity = entityChange.EntityEntry.As <EntityEntry>().Entity; switch (entityChange.ChangeType) { case EntityChangeType.Created: return((entity as IHasCreationTime)?.CreationTime ?? Clock.Now); case EntityChangeType.Deleted: return((entity as IHasDeletionTime)?.DeletionTime ?? Clock.Now); case EntityChangeType.Updated: return((entity as IHasModificationTime)?.LastModificationTime ?? Clock.Now); default: Logger.Error("Unexpected EntityState!"); return(Clock.Now); } }