private string GetTemporalTableSqlDependingOnIfTableIsTemporalOrNot <TEntity>( ITableHelper <FakeDataContext> tableHelper, params Type[] entityTypesToCreateTemporalTablesFor) where TEntity : class { var options = DbContextOptionsSetup.Setup <FakeDataContext>(); using (var context = new FakeDataContext(options)) { // Register entity types to support temporal tables. entityTypesToCreateTemporalTablesFor ?.ToList() ?.ForEach(e => TemporalEntitiesCache.Add(context.Model.FindEntityType(e))); var entityType = context.Model.FindEntityType(typeof(TEntity)); var temporalTableSqlBuilder = new TemporalTableSqlBuilder <FakeDataContext>( context, new TemporalTableSqlGeneratorFactory(), tableHelper); string sql = temporalTableSqlBuilder.BuildTemporalTablesSqlForEntityTypes(new[] { entityType }); return(sql); } }
private string BuildTemporalTableSqlFromEntityTypeConfiguration(IEntityType entityType, bool appendSeparator) { StringBuilder sqlBuilder = new StringBuilder(); var relationalEntityType = context.Model.FindEntityType(entityType.Name); if (relationalEntityType is IEntityType) { string tableName = relationalEntityType.GetTableName(); string schema = relationalEntityType.GetSchema() ?? "dbo"; bool isEntityConfigurationTemporal = TemporalEntitiesCache.IsEntityConfigurationTemporal(entityType); bool isEntityTemporalInDatabase = tableHelper.IsTableTemporal(tableName, schema); ITemporalTableSqlGenerator temporalTableSqlGenerator = temporalTableSqlGeneratorFactory .CreateInstance(isEntityConfigurationTemporal, isEntityTemporalInDatabase, tableName, schema); string temporalTableSql = temporalTableSqlGenerator.Generate(); if (!string.IsNullOrWhiteSpace(temporalTableSql)) { sqlBuilder.AppendLine(temporalTableSql); if (appendSeparator) { sqlBuilder.AppendLine(new string('-', 100)); } } } return(sqlBuilder.ToString()); }
/// <summary> /// Configure none of the tables to be temporal by default. /// You can still register them manually by using <see cref="EntityTypeBuilderExtensions.UseTemporalTable" /> method. /// </summary> public static void PreventTemporalTables(this ModelBuilder modelBuilder) { var entityTypes = modelBuilder.Model.GetEntityTypes().ToList(); foreach (var entityType in entityTypes) { TemporalEntitiesCache.Remove(entityType); } }
/// <summary> /// Validates that temporal tables are enabled on the provided data set. /// </summary> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="dbSet">The database set.</param> private static void ValidateDbSet <TEntity>(DbSet <TEntity> dbSet) where TEntity : class { var entityType = GetEntityType(dbSet); if (!TemporalEntitiesCache.IsEntityConfigurationTemporal(entityType)) { throw new ArgumentException(FormattableString.Invariant($"The entity '{entityType.DisplayName()}' is not using temporal tables."), nameof(dbSet)); } }
/// <summary> /// Configure all tables to be temporal by default. /// You may skip this method and register them manually by using <see cref="EntityTypeBuilderExtensions.UseTemporalTable" /> method. /// </summary> public static void UseTemporalTables(this ModelBuilder modelBuilder) { var entityTypes = modelBuilder.Model.GetEntityTypes() .Where(e => !e.IsOwned()) .ToList(); foreach (var entityType in entityTypes) { TemporalEntitiesCache.Add(entityType); } }
public void AddTemporalTableTest() { var options = DbContextOptionsSetup.Setup <FakeDataContext>(); using (var context = new FakeDataContext(options)) { var entityType = context.Model.FindEntityType(typeof(User)); // Add entity type. TemporalEntitiesCache.Add(entityType); // Test if the entity type exists in the cache. Assert.IsTrue(TemporalEntitiesCache.IsEntityConfigurationTemporal(entityType)); } }
/// <summary> /// Configure not using a temporal table for the specified entity type configuration. /// </summary> public static void PreventTemporalTable <TEntity>(this EntityTypeBuilder <TEntity> entity) where TEntity : class { TemporalEntitiesCache.Remove(entity.Metadata); }
/// <summary> /// Configure temporal table for the specified entity type configuration. /// </summary> public static void UseTemporalTable <TEntity>(this EntityTypeBuilder <TEntity> entity) where TEntity : class { TemporalEntitiesCache.Add(entity.Metadata); }
/// <inheritdoc /> public string BuildTemporalTablesSql(bool appendSeparator = true) { var entityTypes = EnsureTemporalEntitiesCacheIsInitialized(() => TemporalEntitiesCache.GetAll()); return(BuildTemporalTablesSqlForEntityTypes(entityTypes)); }
public void Setup() { TemporalEntitiesCache.Clear(); }