microsoft/aspire · error · InvalidOperationException
A DbContext could not be configured with this…
Error message
A DbContext could not be configured with this AddCosmosDbContext overload. Ensure the connection string '{connectionName}' contains a database name or use the overload that takes a database name as a parameter. What it means
The parameterless AddCosmosDbContext overload infers the database name from the connection string; when parsing yields no database name, EF Core cannot be wired to Cosmos and the extension throws. This overload contract requires 'Database=' in the connection string, otherwise the overload accepting a databaseName parameter must be used.
Solutions
- Add 'Database=<name>' to the connection string.
- Or switch to the AddCosmosDbContext<TContext>(builder, connectionName, databaseName) overload.
- Or set the database name in the 'Aspire:Microsoft:EntityFrameworkCore:Cosmos' configuration section if supported by that path.
Example fix
// before
builder.AddCosmosDbContext<OrderContext>("cosmosdb"); // no Database= in string
// after
builder.AddCosmosDbContext<OrderContext>("cosmosdb", "orders-db"); Defensive patterns
Strategy: validation
Validate before calling
// before AddCosmosDbContext without a databaseName parameter
var cs = builder.Configuration.GetConnectionString(connectionName);
if (cs is not null && !cs.Contains("Database="))
{
throw new InvalidOperationException("Use the AddCosmosDbContext overload with an explicit database name.");
} Type guard
bool ConnectionStringHasDatabase(string? cs) => cs?.Split(';').Any(p => p.TrimStart().StartsWith("Database=", StringComparison.OrdinalIgnoreCase)) == true; Try / catch
try { builder.AddCosmosDbContext<OrderContext>("cosmosdb"); }
catch (InvalidOperationException ex) when (ex.Message.Contains("AddCosmosDbContext overload")) { builder.AddCosmosDbContext<OrderContext>("cosmosdb", "orders-db"); } Prevention
- Prefer the explicit databaseName overload; it makes intent obvious and config-independent.
- Keep Database= in connection strings when using the inferred overload.
- Review connection strings when regenerating them from tooling or CI secrets.
When it happens
Trigger: builder.AddCosmosDbContext<MyContext>(connectionName) where the connection string in 'ConnectionStrings:{connectionName}' has no 'Database=' key and no databaseName parameter overload was used.
Common situations: Connection string contains only endpoint + key; developer swapped from the named-database overload to the inferred one during refactoring; config value regenerated by tooling dropped the Database attribute.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- A Container could not be configured. Ensure valid…
- A database name is required but was not provided. Specify…
- A DbContext could not be configured. Ensure valid…
- Conflicting values for 'RequestTimeout' were found in
- A BlobServiceClient could not be configured. Ensure valid…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/fa19fd58bab2fe6f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos/AspireAzureEFCoreCosmosExtensions.cs:54
public static void AddCosmosDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
Action<EntityFrameworkCoreCosmosSettings>? configureSettings = null,
Action<DbContextOptionsBuilder>? configureDbContextOptions = null) where TContext : DbContext
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(connectionName);
string? databaseName = null;
if (builder.Configuration.GetConnectionString(connectionName) is string connectionString)
{
var cosmosConnectionInfo = CosmosUtils.ParseConnectionString(connectionString);
databaseName = cosmosConnectionInfo.DatabaseName;
}
if (databaseName is null)
{
throw new InvalidOperationException(
"A DbContext could not be configured with this AddCosmosDbContext overload. "
+ $"Ensure the connection string '{connectionName}' contains a database name or use the overload that takes a database name as a parameter.");
}
AddCosmosDbContext<TContext>(
builder,
connectionName,
databaseName,
configureSettings,
configureDbContextOptions);
}
/// <summary>
/// Registers the given <see cref="DbContext" /> as a service in the services provided by the <paramref name="builder"/>.
/// Enables db context pooling, logging and telemetry.
/// </summary>
/// <typeparam name="TContext">The <see cref="DbContext" /> that needs to be registered.</typeparam>
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>View on GitHub (pinned to 25830f84bd)