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

dotnet-ef migrations script omits required GO separators in EF Core 9, causing SQL batch errors #35731

Open
redJ4y opened this issue Mar 5, 2025 · 0 comments

Comments

@redJ4y
Copy link

redJ4y commented Mar 5, 2025

Bug description

We effectively do stored procedure migrations like:

        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(
                """
                CREATE OR ALTER PROCEDURE SomeProcedure
                AS
                    BEGIN TRANSACTION
                    -- Example operation
                    INSERT INTO ExampleTable (Column1, Column2)
                    VALUES ('Value1', 'Value2');
                    COMMIT TRANSACTION
                GO
                """
            );
        }

Running dotnet-ef migrations script version 8.0.8 results in the following script:

BEGIN TRANSACTION;
GO

CREATE OR ALTER PROCEDURE SomeProcedure
AS
    BEGIN TRANSACTION
    -- Example operation
    INSERT INTO ExampleTable (Column1, Column2)
    VALUES ('Value1', 'Value2');
    COMMIT TRANSACTION
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'ExampleStoredProcedureMigration', N'8.0.8');
GO

COMMIT;
GO

However, running the same command on version 9.0.2 results in the following script:

BEGIN TRANSACTION;
CREATE OR ALTER PROCEDURE SomeProcedure
AS
    BEGIN TRANSACTION
    -- Example operation
    INSERT INTO ExampleTable (Column1, Column2)
    VALUES ('Value1', 'Value2');
    COMMIT TRANSACTION

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'ExampleStoredProcedureMigration', N'9.0.2');

COMMIT;
GO

Which is invalid due to the error [S0001][111] 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.

We can work around the issue for now using migrationBuilder.Sql(sql, suppressTransaction: true), but that is not ideal.

This regression does not impact migration bundles—only the dotnet-ef migrations script tool appears to be affected.

Your code

    /// <inheritdoc />
    public partial class ExampleStoredProcedureMigration : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(
                """
                CREATE OR ALTER PROCEDURE SomeProcedure
                AS
                    BEGIN TRANSACTION
                    -- Example operation
                    INSERT INTO ExampleTable (Column1, Column2)
                    VALUES ('Value1', 'Value2');
                    COMMIT TRANSACTION
                GO
                """
            );
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // ...
        }
    }

EF Core version

9.0.2

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 9

Operating system

Windows 11

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