Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Core 6.0 temporal tables - migration rollback doesn't work #27375

Closed
mtaghavi2005 opened this issue Feb 4, 2022 · 1 comment
Closed

EF Core 6.0 temporal tables - migration rollback doesn't work #27375

mtaghavi2005 opened this issue Feb 4, 2022 · 1 comment
Assignees
Labels
area-temporal-tables closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@mtaghavi2005
Copy link

mtaghavi2005 commented Feb 4, 2022

By adding Temporal Table to an existing table (with different schema than default), the applied migration cannot be rolled back.

  • Add Temporal Table to a table with schema "CMN"
            modelBuilder
                .Entity<Employee>()
                .ToTable("Employees","CMN", b => b.IsTemporal());
  • Add a new Migration
public partial class addHistoryTable : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterTable(
                name: "Employees",
                schema: "CMN")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "EmployeesHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", "CMN")
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.AddColumn<DateTime>(
                name: "PeriodEnd",
                schema: "CMN",
                table: "Employees",
                type: "datetime2",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified))
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.AddColumn<DateTime>(
                name: "PeriodStart",
                schema: "CMN",
                table: "Employees",
                type: "datetime2",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified))
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "PeriodEnd",
                schema: "CMN",
                table: "Employees")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "EmployeesHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", "CMN")
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.DropColumn(
                name: "PeriodStart",
                schema: "CMN",
                table: "Employees")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "EmployeesHistory")
                .Annotation("SqlServer:TemporalHistoryTableSchema", "CMN")
                .Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");

            migrationBuilder.AlterTable(
                name: "Employees",
                schema: "CMN")
                .OldAnnotation("SqlServer:IsTemporal", true)
                .OldAnnotation("SqlServer:TemporalHistoryTableName", "EmployeesHistory")
                .OldAnnotation("SqlServer:TemporalHistoryTableSchema", "CMN")
                .OldAnnotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
                .OldAnnotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");
        }
    }
  • Update-Database

  • Rollback the migration

Update-Database -Migration 20220204152523_init

image

It seems the schema is missing in the generated SQL for dropping the history table.

image

Sample Project:
EfCore6-TemporalTables.zip

EF Core version: 6.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
@maumar maumar self-assigned this Feb 4, 2022
maumar added a commit that referenced this issue Feb 4, 2022
…n't work

Problem was that we were incorrectly using old table schema rather than history table schema when we were creating DropTableOperation for the history table, after versioning has been disabled.
Fix is to use the correct value.

Fixes #27375
maumar added a commit that referenced this issue Feb 4, 2022
…n't work

Problem was that we were incorrectly using old table schema rather than history table schema when we were creating DropTableOperation for the history table, after versioning has been disabled.
Fix is to use the correct value.

Fixes #27375
@maumar maumar added this to the 6.0.x milestone Feb 4, 2022
@maumar maumar added the type-bug label Feb 4, 2022
@maumar
Copy link
Contributor

maumar commented Feb 4, 2022

We are using actual table schema, rather than history table schema when we generate the DROP TABLE for history table.

@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 4, 2022
maumar added a commit that referenced this issue Feb 7, 2022
…n't work

Problem was that we were incorrectly using old table schema rather than history table schema when we were creating DropTableOperation for the history table, after versioning has been disabled.
Fix is to use the correct value.

Fixes #27375
@AndriySvyryd AndriySvyryd modified the milestones: 6.0.x, 6.0.4 Feb 15, 2022
dougbu pushed a commit that referenced this issue Mar 2, 2022
…n't work (#27379)

Problem was that we were incorrectly using old table schema rather than history table schema when we were creating DropTableOperation for the history table, after versioning has been disabled.
Fix is to use the correct value.

Fixes #27375
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-temporal-tables closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

4 participants