Dynamic Connection Strings in EF Core 9 Minimal APIs
Altering a DbContext Connection at Runtime
In Entity Framework Core (including EF Core 9.x), the database connection is normally configured when the DbContext is
constructed, and it isn’t intended to be changed afterward. Once a DbContext is injected (e.g. via DI in a minimal
API), its DbContextOptions (including the connection string) are essentially fixed. There is no built-in method to
reconfigure the context’s connection string after it’s been created in the DI container. In other words, you cannot
directly “swap out” the connection string of an existing context instance once it’s been configured.