{"record":{"id":"fa19fd58bab2fe6f","repo":"microsoft/aspire","slug":"a-dbcontext-could-not-be-configured-with-this","errorCode":null,"errorMessage":"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.","messagePattern":"A DbContext could not be configured with this AddCosmosDbContext overload\\. Ensure the connection string '(.+?)' contains a database name or use the overload that takes a database name as a parameter\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos/AspireAzureEFCoreCosmosExtensions.cs","lineNumber":54,"sourceCode":"    public static void AddCosmosDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(\n        this IHostApplicationBuilder builder,\n        string connectionName,\n        Action<EntityFrameworkCoreCosmosSettings>? configureSettings = null,\n        Action<DbContextOptionsBuilder>? configureDbContextOptions = null) where TContext : DbContext\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentException.ThrowIfNullOrEmpty(connectionName);\n\n        string? databaseName = null;\n        if (builder.Configuration.GetConnectionString(connectionName) is string connectionString)\n        {\n            var cosmosConnectionInfo = CosmosUtils.ParseConnectionString(connectionString);\n            databaseName = cosmosConnectionInfo.DatabaseName;\n        }\n\n        if (databaseName is null)\n        {\n            throw new InvalidOperationException(\n                \"A DbContext could not be configured with this AddCosmosDbContext overload. \"\n                + $\"Ensure the connection string '{connectionName}' contains a database name or use the overload that takes a database name as a parameter.\");\n        }\n\n        AddCosmosDbContext<TContext>(\n            builder,\n            connectionName,\n            databaseName,\n            configureSettings,\n            configureDbContextOptions);\n    }\n\n    /// <summary>\n    /// Registers the given <see cref=\"DbContext\" /> as a service in the services provided by the <paramref name=\"builder\"/>.\n    /// Enables db context pooling, logging and telemetry.\n    /// </summary>\n    /// <typeparam name=\"TContext\">The <see cref=\"DbContext\" /> that needs to be registered.</typeparam>\n    /// <param name=\"builder\">The <see cref=\"IHostApplicationBuilder\" /> to read config from and add services to.</param>","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos/AspireAzureEFCoreCosmosExtensions.cs#L36-L72","documentation":"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.","triggerScenarios":"builder.AddCosmosDbContext<MyContext>(connectionName) where the connection string in 'ConnectionStrings:{connectionName}' has no 'Database=' key and no databaseName parameter overload was used.","commonSituations":"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.","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."],"exampleFix":"// before\nbuilder.AddCosmosDbContext<OrderContext>(\"cosmosdb\"); // no Database= in string\n// after\nbuilder.AddCosmosDbContext<OrderContext>(\"cosmosdb\", \"orders-db\");","handlingStrategy":"validation","validationCode":"// before AddCosmosDbContext without a databaseName parameter\nvar cs = builder.Configuration.GetConnectionString(connectionName);\nif (cs is not null && !cs.Contains(\"Database=\"))\n{\n    throw new InvalidOperationException(\"Use the AddCosmosDbContext overload with an explicit database name.\");\n}","typeGuard":"bool ConnectionStringHasDatabase(string? cs) => cs?.Split(';').Any(p => p.TrimStart().StartsWith(\"Database=\", StringComparison.OrdinalIgnoreCase)) == true;","tryCatchPattern":"try { builder.AddCosmosDbContext<OrderContext>(\"cosmosdb\"); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AddCosmosDbContext overload\")) { builder.AddCosmosDbContext<OrderContext>(\"cosmosdb\", \"orders-db\"); }","preventionTips":["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."],"tags":["ef-core","azure-cosmos","connection-string","missing-database-name"],"backgroundTag":"missing-required-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}