diff --git a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs index 5397181e7a8..2caaa043496 100644 --- a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs +++ b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs @@ -1582,6 +1582,18 @@ private enum ParsingState /// The command builder to use to build the commands. protected override void Generate(SqlOperation operation, IModel? model, MigrationCommandListBuilder builder) { + if (Options.HasFlag(MigrationsSqlGenerationOptions.Script)) + { + builder.Append(operation.Sql); + if (!operation.Sql.EndsWith('\n')) + { + builder.AppendLine(); + } + + EndStatement(builder, operation.SuppressTransaction); + return; + } + var preBatched = operation.Sql .Replace("\\\n", "") .Replace("\\\r\n", "") diff --git a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs index 2984e17b476..d0f28e435dd 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs @@ -144,11 +144,11 @@ SELECT @Counter END; GO - SELECT GetDate(); --GO SELECT GetDate() GO +GO INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'00000000000004_Migration4', N'7.0.0-test'); @@ -320,11 +320,11 @@ SELECT @Counter END; GO - SELECT GetDate(); --GO SELECT GetDate() GO +GO INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'00000000000004_Migration4', N'7.0.0-test'); diff --git a/test/EFCore.SqlServer.FunctionalTests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs b/test/EFCore.SqlServer.FunctionalTests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs index effa2a16655..739869558b9 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs @@ -1311,6 +1311,28 @@ public virtual void SqlOperation_handles_multiple_delimiters_in_string() """); } + [ConditionalFact] + public virtual void SqlOperation_handles_go_in_script_with_suppress_transaction() + { + Generate( + new SqlOperation + { + Sql = "CREATE PROCEDURE dbo.Proc1 AS SELECT 1;" + EOL + "GO" + EOL + + "CREATE VIEW view1 AS SELECT 1 AS Id;" + EOL + "GO 2" + EOL + "SELECT 1;", + SuppressTransaction = true + }, + MigrationsSqlGenerationOptions.Script); + + AssertSql( + """ +CREATE PROCEDURE dbo.Proc1 AS SELECT 1; +GO +CREATE VIEW view1 AS SELECT 1 AS Id; +GO 2 +SELECT 1; +"""); + } + public override void InsertDataOperation_all_args_spatial() { base.InsertDataOperation_all_args_spatial();