Skip to content

Add setting to make migration script creation use EXEC(string) for migrationBuilder.Sql(...) generation #32526

Closed as not planned

Description

When creating an idempotent migration script, migrationBuilder.Sql(...) outputs TSQL that looks like the following, for example ...

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20231117070637_BoxLogfileToZipfile')
BEGIN
                    UPDATE BoxLogfiles
                    SET BoxZipfileId = u.BoxZipfileId
                    FROM RowsToUpdate AS u
                    WHERE Id = u.BoxLogfileId;
END;
GO

This makes it so that SQL Server still evaluates the SQL expression and it will fail even if the IF NOT EXISTS statements is not met. This can cause issues applying idempotent migrations to DBs that already have them.

Perhaps add a setting to make the output use EXEC(string) instead? Like the following, as suggested here ...

IF COL_LENGTH('Database_Name.dbo.Table_Name', 'Column_Name1') IS NOT NULL
BEGIN
    DECLARE @sql VARCHAR(MAX)
    SET @sql = 'UPDATE Table_Name
            SET Column_Name2 = (SELECT Column_Name3 FROM Table_Name2
                WHERE Column_Name4 = ''Some Value'')
            WHERE Column_Name5 IS NULL;

        UPDATE Table_Name
            SET Column_Name6 = Column_Name1
            WHERE Column_Name6 IS NULL;'
     EXEC(@sql)
END

I see that the documentation for efcore also mentions what I'm requesting here. How would I do this manually in the migration C# though? Rewrite the actual query to include EXEC? Why not just a feature that efcore would allow me to choose between the output style?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions