Skip to content
47 changes: 46 additions & 1 deletion src/Aspire.Hosting.EntityFrameworkCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,54 @@

Similarly you can use `PublishAsMigrationScript()` if you also want a raw SQL script produced.

#### Troubleshooting: design-time connection strings

Generating a migration bundle runs the EF Core design-time tooling, which creates
your `DbContext` to discover the model. Providers that parse the connection string
while the context is being created — for example the Azure Npgsql integration
(`EnrichAzureNpgsqlDbContext`) — fail at design time because no real connection
string is available, producing an error such as:

```

Check failure on line 261 in src/Aspire.Hosting.EntityFrameworkCore/README.md

View workflow job for this annotation

GitHub Actions / lint

Fenced code blocks should have a language specified [Context: "```"]
Unable to create a 'DbContext' of type 'AppDbContext'. The exception 'Format of the
initialization string does not conform to specification starting at index 0.' was
thrown while attempting to create an instance.
```

Guard the connection-string-dependent configuration with `EF.IsDesignTime` so EF
Core can build the model without a connection string. The bundle still receives the
real connection string at run time via the `--connection` argument Aspire injects:

```csharp
builder.Services.AddPooledDbContextFactory<AppDbContext>((sp, options) =>
{
if (EF.IsDesignTime)
{
// At design time (e.g. when generating a migration bundle) no real
// connection string is available. Use the overload that doesn't take a
// connection string so EF Core can build the model without parsing one.
options.UseNpgsql(o => { });
}
else
{
options.UseNpgsql(builder.Configuration.GetConnectionString("postgresdb"));
}
});

if (!EF.IsDesignTime)
{
// EnrichAzureNpgsqlDbContext configures an Entra ID data source that parses
// the connection string, which is not available at design time.
builder.EnrichAzureNpgsqlDbContext<AppDbContext>();
}
```

The same pattern applies to any provider that parses the connection string while the
`DbContext` is being created (for example a custom `NpgsqlDataSource`).

## Additional documentation

<!-- TODO: Update this to the EntityFrameworkCore-specific page once published, https://github.com/microsoft/aspire.dev/issues/536 -->
https://aspire.dev/integrations/databases/efcore/migrations/#automated-ef-migrations-with-addefmigrations
https://learn.microsoft.com/dotnet/aspire/
Comment thread
AndriySvyryd marked this conversation as resolved.
Resolved
https://learn.microsoft.com/ef/core/managing-schemas/migrations/

Expand Down
Loading