You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix issues with line breaks in string literals #1478
The below is an AI-generated analysis and may contain inaccuracies.
Confirmed on released Npgsql provider 10.0.3, 8.0.11, and current main (59473359), executing generated idempotent migration scripts against an isolated PostgreSQL 18 instance. Reading the database back confirms that string defaults, inserted seed data, table comments, and column comments all acquire indentation, and CRLF is normalized to LF. For example, "Don't\r\nGO\r\n this way\\n\r\n" becomes "Don't\n GO\n this way\\n\n ". This is a longstanding provider bug, not an identified recent regression; no last-known-working version was established.
EF's migration script builder indents command text and normalizes physical line endings. Npgsql currently emits physical CR/LF inside ordinary string literals, so this formatting changes the value. The PostgreSQL-specific fix is to use an escape string literal, e.g. E'Don''t\r\nGO\r\n', for strings containing CR/LF, escaping existing backslashes as well. This is still a literal, so unlike concatenation it is valid in COMMENT ON ... IS ... and other literal-only contexts.
dotnet/efcore#20391 tracks user-supplied HasDefaultValueSql/HasComputedColumnSql. Those raw SQL expressions are separate from generated string literals and are outside this fix.
No covering Npgsql PR was found. #1479 is a different, merged serial-to-identity fix.
minimal repro
Console application referencing Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3 and Testcontainers.PostgreSql 4.15.0, with a Docker-compatible runtime:
usingMicrosoft.EntityFrameworkCore;usingMicrosoft.EntityFrameworkCore.Infrastructure;usingMicrosoft.EntityFrameworkCore.Migrations;usingMicrosoft.Extensions.Logging;usingNpgsql;usingTestcontainers.PostgreSql;awaitusingvarpostgres=newPostgreSqlBuilder("postgres:18").Build();awaitpostgres.StartAsync();Environment.SetEnvironmentVariable("Test__Npgsql__DefaultConnection",postgres.GetConnectionString());awaitusingvarcontext=newTestContext();varscript=context.GetService<IMigrator>().GenerateScript(options:MigrationsSqlGenerationOptions.Idempotent);awaitcontext.Database.ExecuteSqlRawAsync(script);awaitcontext.Database.ExecuteSqlRawAsync("""INSERT INTO "Blogs" ("Id") VALUES (2)""");awaitusingvarconnection=newNpgsqlConnection(postgres.GetConnectionString());awaitconnection.OpenAsync();foreach(varsqlinnew[]{"""SELECT "Name" FROM "Blogs" WHERE "Id" = 1""","""SELECT "Name" FROM "Blogs" WHERE "Id" = 2""","""SELECT obj_description('"Blogs"'::regclass)""","""SELECT col_description('"Blogs"'::regclass, 2)"""}){awaitusingvarcommand=newNpgsqlCommand(sql,connection);varactual=(string)(awaitcommand.ExecuteScalarAsync())!;Console.WriteLine($"{System.Text.Json.JsonSerializer.Serialize(actual)}; exact match: {actual==TestContext.Value}");}publicclassTestContext:DbContext{publicconststringValue="Don't\r\nGO\r\n this way\\n\r\n";protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection")).LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();}[DbContext(typeof(TestContext))][Migration("20261021000000_Multiline")]publicclassMultilineMigration:Migration{protectedoverridevoidUp(MigrationBuildermigrationBuilder){migrationBuilder.CreateTable("Blogs",columns: table =>new{Id=table.Column<int>(nullable:false),Name=table.Column<string>(type:"text",nullable:false,defaultValue:TestContext.Value,comment:TestContext.Value)},constraints: table =>table.PrimaryKey("PK_Blogs", x =>x.Id),comment:TestContext.Value);migrationBuilder.InsertData("Blogs",["Id","Name"],["integer","text"],[1,TestContext.Value]);}}
Activity
Handle line breaks in string literals
Handle line breaks in string literals
roji commentedon Oct 21, 2026
AI Triage
The below is an AI-generated analysis and may contain inaccuracies.
Confirmed on released Npgsql provider 10.0.3, 8.0.11, and current
main(59473359), executing generated idempotent migration scripts against an isolated PostgreSQL 18 instance. Reading the database back confirms that string defaults, inserted seed data, table comments, and column comments all acquire indentation, and CRLF is normalized to LF. For example,"Don't\r\nGO\r\n this way\\n\r\n"becomes"Don't\n GO\n this way\\n\n ". This is a longstanding provider bug, not an identified recent regression; no last-known-working version was established.EF's migration script builder indents command text and normalizes physical line endings. Npgsql currently emits physical CR/LF inside ordinary string literals, so this formatting changes the value. The PostgreSQL-specific fix is to use an escape string literal, e.g.
E'Don''t\r\nGO\r\n', for strings containing CR/LF, escaping existing backslashes as well. This is still a literal, so unlike concatenation it is valid inCOMMENT ON ... IS ...and other literal-only contexts.Related work:
HasDefaultValueSql/HasComputedColumnSql. Those raw SQL expressions are separate from generated string literals and are outside this fix.minimal repro
Console application referencing
Npgsql.EntityFrameworkCore.PostgreSQL10.0.3 andTestcontainers.PostgreSql4.15.0, with a Docker-compatible runtime:All four exact-match checks return
False.