diff --git a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs index 096abfdaf2c..cd568c99827 100644 --- a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs +++ b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs @@ -2573,12 +2573,25 @@ alterTableOperation.OldTable[SqlServerAnnotationNames.TemporalHistoryTableSchema if (historyTableName != null) { - operations.Add( - new DropTableOperation - { - Name = historyTableName, - Schema = alterTableOperation.OldTable.Schema - }); + var useOldBehavior27375 = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27375", out var enabled27375) && enabled27375; + if (!useOldBehavior27375) + { + operations.Add( + new DropTableOperation + { + Name = historyTableName, + Schema = historyTableSchema + }); + } + else + { + operations.Add( + new DropTableOperation + { + Name = historyTableName, + Schema = alterTableOperation.OldTable.Schema + }); + } } operations.Add(operation); diff --git a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs index a0623d173fb..e810514f565 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs @@ -3015,6 +3015,61 @@ FROM [sys].[default_constraints] [d] @"DROP TABLE [HistoryTable];"); } + [ConditionalFact] + public virtual async Task Convert_temporal_table_with_explicit_history_table_schema_to_normal_table() + { + await Test( + builder => builder.Entity( + "Customer", e => + { + e.Property("Id").ValueGeneratedOnAdd(); + e.Property("Name"); + e.Property("PeriodStart").ValueGeneratedOnAddOrUpdate(); + e.Property("PeriodEnd").ValueGeneratedOnAddOrUpdate(); + e.HasKey("Id"); + + e.ToTable(tb => tb.IsTemporal(ttb => + { + ttb.UseHistoryTable("HistoryTable", "historySchema"); + ttb.HasPeriodStart("PeriodStart"); + ttb.HasPeriodEnd("PeriodEnd"); + })); + }), + builder => builder.Entity( + "Customer", e => + { + e.Property("Id").ValueGeneratedOnAdd(); + e.Property("Name"); + e.Property("PeriodStart"); + e.Property("PeriodEnd"); + e.HasKey("Id"); + }), + model => + { + var table = Assert.Single(model.Tables); + Assert.Equal("Customer", table.Name); + Assert.Null(table[SqlServerAnnotationNames.IsTemporal]); + Assert.Null(table[SqlServerAnnotationNames.TemporalHistoryTableName]); + + Assert.Collection( + table.Columns, + c => Assert.Equal("Id", c.Name), + c => Assert.Equal("Name", c.Name), + c => Assert.Equal("PeriodEnd", c.Name), + c => Assert.Equal("PeriodStart", c.Name)); + Assert.Same( + table.Columns.Single(c => c.Name == "Id"), + Assert.Single(table.PrimaryKey!.Columns)); + }); + + AssertSql( + @"ALTER TABLE [Customer] SET (SYSTEM_VERSIONING = OFF)", + // + @"ALTER TABLE [Customer] DROP PERIOD FOR SYSTEM_TIME", + // + @"DROP TABLE [historySchema].[HistoryTable];"); + } + [ConditionalFact] public virtual async Task Convert_normal_table_to_temporal_table_with_minimal_configuration() {