SQL Server Migrations - SqlOperation.Sql is stripped of all empty lines #32730
Labels
area-migrations
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
Servicing-approved
type-bug
Milestone
We've upgraded to EF Core 8.0.0 (from 7.0.8) and have started experiencing an issue with
MigrationBuilder.Sql
where all empty lines are dropped from the SQL executed by the resulting migration. This seems to be a result of the change inSqlServerMigrationsSqlGenerator.Generate(SqlOperation operation...)
that was done to fix a regex timeout.In the current code, it seems that new
string.IsNullOrWhiteSpace(line)
check is causing this issue. Here's the related snippet from SqlServerMigrationsSqlGenerator:While some empty lines could be considered inconsequential, some are not, and currently all of them seem to be removed. For example, a migration like this:
Results in the below SQL execution (note the missing empty line):
Sample Code w/ Issue
Another of our current use cases involves using
MigrationBuilder.Sql
to manage changes to stored procedures and other db objects, and the dropped empty lines reduce readability of the definitions for these objects in the db.My current workaround is to create a separate generator that inherits from
SqlServerMigrationsSqlGenerator
, override theGenerate(SqlOperation operation...)
method with the addition ofbatchBuilder.AppendLine(line)
inside of the aforementionedif
block, and replacing theIMigrationsSqlGenerator
service with this new implementation viaUseSqlServer().ReplaceService<>
. However, it would be awesome to not have to do that as we're losing potential future optimizations of the generator and introducing potential for incompatibility and maintenance overhead for our future EF Core updates.EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11 Pro 22H2
IDE: Visual Studio 2022 17.8.3
The text was updated successfully, but these errors were encountered: