Skip to content

Renaming and dropping columns with Temporal Tables generates faulty migrationΒ #27423

@zengouu

Description

@zengouu

Renaming and dropping columns in the same migration when using EF Core 6 supported Temporal Tables results in non-working migration syntax. Ordering of drop and rename (see code below) results in following error:

Setting SYSTEM_VERSIONING to ON failed because column 'RemovedTS' at ordinal 6 in history table 'history.GeneralVendorContact' has a different name than the column 'EndedTS' at the same ordinal in table 'prc.GeneralVendorContact'.

        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "IsRemoved",
                schema: "prc",
                table: "GeneralVendorContact")
                .Annotation("SqlServer:IsTemporal", true)
                .Annotation("SqlServer:TemporalHistoryTableName", "GeneralVendorContact")
                .Annotation("SqlServer:TemporalHistoryTableSchema", "history");

            migrationBuilder.RenameColumn(
                name: "RemovedTS",
                schema: "prc",
                table: "GeneralVendorContact",
                newName: "EndedTS");

            migrationBuilder.RenameColumn(
                name: "RemovedByUserId",
                schema: "prc",
                table: "GeneralVendorContact",
                newName: "EndedByUserId");
        }

This can be fixed by manually editing the generated Up() method and switching the migration to first rename and then drop columns, as follows:

        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.RenameColumn(
                name: "RemovedTS",
                schema: "prc",
                table: "GeneralVendorContact",
                newName: "EndedTS");

            migrationBuilder.RenameColumn(
                name: "RemovedByUserId",
                schema: "prc",
                table: "GeneralVendorContact",
                newName: "EndedByUserId");

            migrationBuilder.DropColumn(
		    name: "IsRemoved",
		    schema: "prc",
		    table: "GeneralVendorContact")
	            .Annotation("SqlServer:IsTemporal", true)
	            .Annotation("SqlServer:TemporalHistoryTableName", "GeneralVendorContact")
	            .Annotation("SqlServer:TemporalHistoryTableSchema", "history");
        }

Versions used

EF Core version: 6.0.1 (all ef related packages @ version 6.0.1)
Database provider: Microsoft.EntityFrameworkCore.SqlServer (6.0.1)
Target framework: NET 6.0
Operating system: Microsoft Windows 11 Pro, Version 10.0.22000 Build 22000
IDE: Microsoft Visual Studio 2022 (64bit) 17.0.6

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions