Skip to content

EF Core 7 AlterColumn generates an UPDATE in the SQL Server migration that can cause issues if the column is renamed in a subsequent migration #29780

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

Closed
ajmoskwa opened this issue Dec 5, 2022 · 1 comment

Comments

@ajmoskwa
Copy link

ajmoskwa commented Dec 5, 2022

AlterColumn appears to have added an UPDATE clause. When using all migrations to generate one big .sql migration file (which we need to do for Devops Pipelines) the migration has a syntax error in it on the UPDATE clause if the column subject to the UPDATE is later renamed in a subsequent migration.

Here is the statement in the migration that causes the issue, (The "County" column was later renamed in a migration that happened after this one)

            migrationBuilder.AlterColumn<short>(
                name: "County",
                table: "Address",
                type: "smallint",
                nullable: false,
                defaultValue: (short)0,
                oldClrType: typeof(short),
                oldType: "smallint",
                oldNullable: true);

Here is the SQL code that EF Core 7 generates for this migration:

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220525102303_ModelUpdates')
BEGIN
    DECLARE @var6 sysname;
    SELECT @var6 = [d].[name]
    FROM [sys].[default_constraints] [d]
    INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
    WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Address]') AND [c].[name] = N'County');
    IF @var6 IS NOT NULL EXEC(N'ALTER TABLE [Address] DROP CONSTRAINT [' + @var6 + '];');
    UPDATE [Address] SET [County] = CAST(0 AS smallint) WHERE [County] IS NULL;
    ALTER TABLE [Address] ALTER COLUMN [County] smallint NOT NULL;
    ALTER TABLE [Address] ADD DEFAULT CAST(0 AS smallint) FOR [County];
END;
GO

Here is the SQL code that EF Core 6 generates that lacks the UPDATE and therefore doesn't cause an error.

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20220525102303_ModelUpdates')
BEGIN
    DECLARE @var6 sysname;
    SELECT @var6 = [d].[name]
    FROM [sys].[default_constraints] [d]
    INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
    WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Address]') AND [c].[name] = N'County');
    IF @var6 IS NOT NULL EXEC(N'ALTER TABLE [Address] DROP CONSTRAINT [' + @var6 + '];');
    ALTER TABLE [Address] ALTER COLUMN [County] smallint NOT NULL;
    ALTER TABLE [Address] ADD DEFAULT CAST(0 AS smallint) FOR [County];
END;
GO

Below is the error shown in SQL studio for this migration. A similar error appears in the pipeline log when attempting to run the migration from Devops.

Msg 207, Level 16, State 1, Line 9
Invalid column name 'County'.

EF Core version: 7
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 7.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.4

@roji
Copy link
Member

roji commented Dec 5, 2022

Duplicate of #29530

@roji roji marked this as a duplicate of #29530 Dec 5, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants