Forward EF migration connection string to the dotnet-ef tool resource - #18452
Conversation
…ol resource Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where AddEFMigrations(...).WithReference(<db>) failed with The ConnectionString property has not been initialized. The root cause is that AddEFMigrationCommands only copied environment annotations from the startup project (eagerly, before .WithReference(<db>) runs), so the connection string declared on the migration resource never reached the hidden dotnet-ef tool resource.
The fix adds a lazy WithEnvironment callback on the tool resource that forwards the migration resource's own EnvironmentCallbackAnnotations at start time, capturing ConnectionStrings__<db> that is added after the command-wiring runs. I verified the forwarded callbacks are evaluated both in run mode (via StartEfToolResourceAsync → ExecutionConfigurationBuilder.WithEnvironmentVariablesConfig()) and when the env is resolved directly, that EFMigrationResource inherits from ContainerResource (so .WithReference adds the expected annotation), and that the lazy callback iterates the migration resource's annotations (not the tool's), so there is no recursion or duplicate-application on the same resource.
Changes:
- Add a lazy
WithEnvironmentcallback to thedotnet-eftool resource that forwards the migration resource's environment callbacks at start time. - Add regression test
ToolResourceInheritsConnectionStringFromMigrationReferenceverifyingef-tool-<name>resolvesConnectionStrings__<db>from the migration reference.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Aspire.Hosting.EntityFrameworkCore/EFResourceBuilderExtensions.cs | Adds the lazy env-forwarding callback so the design-time dotnet-ef process receives the migration resource's connection string. |
| tests/Aspire.Hosting.EntityFrameworkCore.Tests/AddEFMigrationsTests.cs | Adds a regression test (and a minimal IResourceWithConnectionString test resource) asserting the connection string flows to the tool resource. |
I did not identify any concrete, high-confidence issues to comment on. The change is small and well-scoped, the regression test exercises the exact code path that was broken, and the surrounding behavior (run vs. publish, project vs. migration env, tool-resource isolation) checks out.
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 18452Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 18452" |
This comment has been minimized.
This comment has been minimized.
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
70e19ae to
ff811e7
Compare
This comment has been minimized.
This comment has been minimized.
PR Testing ReportPR Information
Artifact Version Verification
Changes AnalyzedFiles Changed
Change Categories
Why templates couldn't be usedThis behavior is not expressible via Test Scenarios ExecutedScenario 1: End-to-end migration on start (run mode) — the exact fixed scenarioObjective: Verify Setup note: The playground's Steps:
Evidence:
Observations:
Scenario 2: Regression unit testObjective: Assert Steps:
Observations:
Unhappy-path / boundary coverageSecond migration resource ( Summary
Overall Result✅ PR VERIFIED The fix correctly forwards Notes / Recommendations
|
ff811e7 to
82bd75e
Compare
Tests selector (audit mode)The full test matrix and all jobs still run in audit mode. The tests and jobs below are what selective CI would run under enforcement. 2 / 98 test projects · 2 jobs, from 10 changed files. Selected test projects (2 / 98)
Selected jobs (2)
How these were chosen — grouped by what changed📄 📄 📄 📄 📄 📄 📄 📄 🔧 🧪 Job reasons
Selection computed for commit |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 10 changed files in this pull request and generated 1 comment.
Files not reviewed (2)
- playground/SqlServerEndToEnd/SqlServerEndToEnd.Common/Db1Migrations/20260511233127_Initial.Designer.cs: Generated file
- playground/SqlServerEndToEnd/SqlServerEndToEnd.Common/Db2Migrations/20260511233305_Initial.Designer.cs: Generated file
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
|
✅ No documentation update needed. Step 5 branch taken: Triggered signals: none (signal_count = 0) Rationale: PR #18452 is a bug fix that corrects The fix adds a lazy
The |
Fixes #18375
Description
AddEFMigrations(...).WithReference(<db>)failed to apply migrations withThe ConnectionString property has not been initialized.because the connection string attached to the migration resource never reached the hiddendotnet-eftool resource.AddEFMigrationCommandsonly copied environment annotations from the startup project, and did so eagerly duringAddEFMigrations— before.WithReference(<db>)is called. When the database is referenced on the migration resource (not the project),ConnectionStrings__<db>was never forwarded to the design-time process.Changes
EFResourceBuilderExtensions.AddEFMigrationCommands: add a lazyWithEnvironmentcallback on the tool resource that forwards the migration resource's own environment callbacks at start time. This capturesConnectionStrings__<db>from.WithReference(<db>), which is added after the command-wiring runs (the eager project-env copy can't see it).ToolResourceInheritsConnectionStringFromMigrationReferenceassertsef-tool-<name>resolvesConnectionStrings__<db>from the migration reference.Note: the
ASPNETCORE_URLS/portForServingline in the issue is a non-fatal DCP substitution warning (endpoint env is unused by design-time EF); the connection-string failure is the actual blocker addressed here.Checklist
<remarks />and<code />elements on your triple slash comments?