diff --git a/src/EFCore.SqlServer/Extensions/SqlServerEntityTypeExtensions.cs b/src/EFCore.SqlServer/Extensions/SqlServerEntityTypeExtensions.cs index 31d8109195d..9d55811eb3e 100644 --- a/src/EFCore.SqlServer/Extensions/SqlServerEntityTypeExtensions.cs +++ b/src/EFCore.SqlServer/Extensions/SqlServerEntityTypeExtensions.cs @@ -278,6 +278,25 @@ public static void SetHistoryTableSchema(this IMutableEntityType entityType, str public static ConfigurationSource? GetHistoryTableSchemaConfigurationSource(this IConventionEntityType entityType) => entityType.FindAnnotation(SqlServerAnnotationNames.TemporalHistoryTableSchema)?.GetConfigurationSource(); + /// + /// Returns the name of the history table to which the entity type is mapped prepended by the schema + /// or if not mapped to a table. + /// + /// The entity type to get the history table name for. + /// The name of the history table to which the entity type is mapped prepended by the schema. + public static string? GetSchemaQualifiedHistoryTableName(this IReadOnlyEntityType entityType) + { + var historyTableName = entityType.GetHistoryTableName(); + if (historyTableName == null) + { + return null; + } + + var schema = entityType.GetHistoryTableSchema(); + + return (string.IsNullOrEmpty(schema) ? "" : schema + ".") + historyTableName; + } + #endregion Temporal table #region SQL OUTPUT clause diff --git a/test/EFCore.SqlServer.FunctionalTests/ModelBuilding/SqlServerModelBuilderTestBase.cs b/test/EFCore.SqlServer.FunctionalTests/ModelBuilding/SqlServerModelBuilderTestBase.cs index 9b20c495e9e..cad38af605b 100644 --- a/test/EFCore.SqlServer.FunctionalTests/ModelBuilding/SqlServerModelBuilderTestBase.cs +++ b/test/EFCore.SqlServer.FunctionalTests/ModelBuilding/SqlServerModelBuilderTestBase.cs @@ -1219,6 +1219,7 @@ public virtual void Temporal_table_default_settings() Assert.True(entity.IsTemporal()); Assert.Equal("CustomerHistory", entity.GetHistoryTableName()); Assert.Null(entity.GetHistoryTableSchema()); + Assert.Equal("CustomerHistory", entity.GetSchemaQualifiedHistoryTableName()); var periodStart = entity.GetProperty(entity.GetPeriodStartPropertyName()!); var periodEnd = entity.GetProperty(entity.GetPeriodEndPropertyName()!); @@ -1257,6 +1258,7 @@ public virtual void Temporal_table_with_history_table_configuration() Assert.Equal("HistoryTable", entity.GetHistoryTableName()); Assert.Equal("historySchema", entity.GetHistoryTableSchema()); + Assert.Equal("historySchema.HistoryTable", entity.GetSchemaQualifiedHistoryTableName()); var periodStart = entity.GetProperty(entity.GetPeriodStartPropertyName()!); var periodEnd = entity.GetProperty(entity.GetPeriodEndPropertyName()!); @@ -1306,6 +1308,7 @@ public virtual void Temporal_table_with_changed_configuration() Assert.Equal("ChangedHistoryTable", entity.GetHistoryTableName()); Assert.Equal("changedHistorySchema", entity.GetHistoryTableSchema()); + Assert.Equal("changedHistorySchema.ChangedHistoryTable", entity.GetSchemaQualifiedHistoryTableName()); var periodStart = entity.GetProperty(entity.GetPeriodStartPropertyName()!); var periodEnd = entity.GetProperty(entity.GetPeriodEndPropertyName()!); @@ -1355,6 +1358,7 @@ public virtual void Temporal_table_with_period_column_names_changed_configuratio Assert.Equal("ChangedHistoryTable", entity.GetHistoryTableName()); Assert.Equal("changedHistorySchema", entity.GetHistoryTableSchema()); + Assert.Equal("changedHistorySchema.ChangedHistoryTable", entity.GetSchemaQualifiedHistoryTableName()); var periodStart = entity.GetProperty(entity.GetPeriodStartPropertyName()!); var periodEnd = entity.GetProperty(entity.GetPeriodEndPropertyName()!); @@ -1404,6 +1408,8 @@ public virtual void Temporal_table_with_explicit_properties_mapped_to_the_period Assert.Equal(7, entity.GetProperties().Count()); Assert.Equal("HistoryTable", entity.GetHistoryTableName()); + Assert.Null(entity.GetHistoryTableSchema()); + Assert.Equal("HistoryTable", entity.GetSchemaQualifiedHistoryTableName()); var periodStart = entity.GetProperty(entity.GetPeriodStartPropertyName()!); var periodEnd = entity.GetProperty(entity.GetPeriodEndPropertyName()!); @@ -1452,6 +1458,8 @@ public virtual void Assert.Equal(9, entity.GetProperties().Count()); Assert.Equal("HistoryTable", entity.GetHistoryTableName()); + Assert.Null(entity.GetHistoryTableSchema()); + Assert.Equal("HistoryTable", entity.GetSchemaQualifiedHistoryTableName()); var periodStart = entity.GetProperty(entity.GetPeriodStartPropertyName()!); var periodEnd = entity.GetProperty(entity.GetPeriodEndPropertyName()!);