Skip to content

Commit f937601

Browse files
nanotaboadaclaude
andcommitted
fix(data): fix Npgsql migrations and suppress PendingModelChangesWarning (#249)
Two bugs caught by Docker Compose sanity check of the PostgreSQL path. - Populate BuildTargetModel in Npgsql seed migration designer files so Npgsql's SQL generator can resolve column types when applying InsertData operations (SeedStarting11, SeedSubstitutes) - Suppress PendingModelChangesWarning for the postgres provider path via ConfigureWarnings; hand-crafted designer files cannot replicate Npgsql-injected runtime annotations (Relational:MaxIdentifierLength, UseIdentityByDefaultColumn), causing a false-positive that aborted MigrateAsync() at startup; BuildTargetModel is still populated so InsertData SQL generation continues to work correctly Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e52abca commit f937601

4 files changed

Lines changed: 115 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ This project uses famous football stadiums (A-Z) that hosted FIFA World Cup matc
6262

6363
### Fixed
6464

65+
- Populate `BuildTargetModel` in Npgsql seed migration designer files so Npgsql's SQL generator can resolve column types when applying `InsertData` operations.
66+
- Suppress `PendingModelChangesWarning` for the postgres provider path — hand-crafted designer files cannot replicate Npgsql-injected runtime annotations (`Relational:MaxIdentifierLength`, `UseIdentityByDefaultColumn`), causing a false-positive that aborted `MigrateAsync()` at startup.
67+
- Normalize `DATABASE_PROVIDER` to lowercase in `entrypoint.sh` via `tr` so `POSTGRES`, `Postgres`, etc. are handled consistently with `AddDbContextPool`.
68+
- Trim and normalize `DATABASE_PROVIDER` in `AddDbContextPool` before the provider switch; add explicit `sqlite`/empty case; throw `InvalidOperationException` for unrecognized values so typos no longer silently fall through to SQLite.
69+
- Move `Npgsql.EntityFrameworkCore.PostgreSQL` package from the "Development dependencies" `ItemGroup` to "Runtime dependencies".
70+
6571
### Removed
6672

6773
---

src/Dotnet.Samples.AspNetCore.WebApi/Extensions/ServiceCollectionExtensions.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Dotnet.Samples.AspNetCore.WebApi.Validators;
1010
using FluentValidation;
1111
using Microsoft.EntityFrameworkCore;
12+
using Microsoft.EntityFrameworkCore.Diagnostics;
1213
using Microsoft.EntityFrameworkCore.Migrations;
1314
using Microsoft.OpenApi;
1415
using Serilog;
@@ -35,9 +36,7 @@ IWebHostEnvironment environment
3536
{
3637
services.AddDbContextPool<PlayerDbContext>(options =>
3738
{
38-
var provider = (
39-
Environment.GetEnvironmentVariable("DATABASE_PROVIDER") ?? ""
40-
)
39+
var provider = (Environment.GetEnvironmentVariable("DATABASE_PROVIDER") ?? "")
4140
.Trim()
4241
.ToLowerInvariant();
4342

@@ -50,6 +49,13 @@ IWebHostEnvironment environment
5049
"DATABASE_URL is required when DATABASE_PROVIDER=postgres."
5150
);
5251
options.UseNpgsql(connectionString);
52+
// Hand-crafted designer files cannot replicate Npgsql-injected runtime
53+
// annotations (Relational:MaxIdentifierLength, UseIdentityByDefaultColumn),
54+
// causing a false-positive PendingModelChangesWarning. Suppressed here;
55+
// BuildTargetModel is still populated so InsertData SQL generation works.
56+
options.ConfigureWarnings(w =>
57+
w.Ignore(RelationalEventId.PendingModelChangesWarning)
58+
);
5359
break;
5460

5561
case "sqlite":

src/Dotnet.Samples.AspNetCore.WebApi/Migrations/Npgsql/20260409151100_SeedStarting11.Designer.cs

Lines changed: 50 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Dotnet.Samples.AspNetCore.WebApi/Migrations/Npgsql/20260409151200_SeedSubstitutes.Designer.cs

Lines changed: 50 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)