AddCosmosDbContext does not work with Azure Functions project without explicitly specifying database name
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
Hi,
I am trying to use the Cosmos DB EFCore integrations with Aspire. Per the documentation here:
https://learn.microsoft.com/en-us/dotnet/aspire/database/azure-cosmos-db-entity-framework-integration?tabs=dotnet-cli#add-cosmos-db-context
If you call `AddCosmosDbContext` without specifying the `databaseName` parameter, it should automatically be inferred as long as your connection string has the database name in it. In your app host, if you call `AddCosmosDatabase`, this adds a connection string with the database name.
However, this appears to not be functional when adding the database reference to an Azure Functions project.
### Expected Behavior
DbContext is correctly configured, and the database name is inferred from the connection string.
### Steps To Reproduce
App host:
```csharp
using Azure.Provisioning.Storage;
using Microsoft.Extensions.Hosting;
var builder = DistributedApplication.CreateBuilder(args);
var hostStorageAccount = builder.AddAzureStorage("cprphoststorage");
// Setup CosmosDB
var cosmos = builder.AddAzureCosmosDB("cprpcosmos");
var database = cosmos.AddCosmosDatabase("cprpcosmosdb");
if (builder.Environment.IsDevelopment())
{
hostStorageAccount.RunAsEmulator();
#pragma warning disable ASPIRECOSMOSDB001
cosmos.RunAsPreviewEmulator(
emulator =>
{
emulator
.WithDataExplorer()
.WithLifetime(ContainerLifetime.Persistent);
});
}
var functions = builder.AddAzureFunctionsProject("cprpfunctions")
.WaitFor(hostStorageAccount)
.WithRoleAssignments(hostStorageAccount, StorageBuiltInRole.StorageBlobDataOwner)
.WithHostStorage(hostStorageAccount)
.WaitFor(cosmos).WithReference(database)
.WithExternalHttpEndpoints();
await builder.Build().RunAsync();
```
Functions `Program.cs`:
```csharp
using CPRP.Functions.Storage;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
var builder = FunctionsApplication.CreateBuilder(args);
builder.AddServiceDefaults()
.ConfigureFunctionsWebApplication()
.AddCosmosDbContext("cprpcosmosdb");
await builder.Build().RunAsync();
```
In the `FunctionsApplicationBuilder` configuration I _can_ see the connection string being passed through:


However, the call to `GetConnectionString` here:
https://github.com/dotnet/aspire/blob/c8522bcdad5d092358f55e88af568a8c879b3147/src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos/AspireAzureEFCoreCosmosExtensions.cs#L47
Returns null, resulting in the exception below.
Instead, if you call the `AddCosmosDbContext` with the database name overload like so:
```csharp
.AddCosmosDbContext("cprpcosmosdb", "cprpcosmosdb");
```
The call to `GetDbContextSettings`:
https://github.com/dotnet/aspire/blob/c8522bcdad5d092358f55e88af568a8c879b3147/src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos/AspireAzureEFCoreCosmosExtensions.cs#L93
_Does_ return an `EntityFrameworkCoreCosmosSettings` with the connection string populated, but again the subsequent call to `builder.Configuration.GetConnectionString` returns null, and we just happen to be saved by the fact that we set the database name to the parameter passed in if we failed to parse the connections string:
https://github.com/dotnet/aspire/blob/c8522bcdad5d092358f55e88af568a8c879b3147/src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos/AspireAzureEFCoreCosmosExtensions.cs#L119
If you need a full GitHub repo repro please let me know and I can provide one.
### Exceptions (if any)
```
System.InvalidOperationException: A DbContext could not be configured with this AddCosmosDbContext overload. Ensure the connection string 'cprpcosmosdb' contains a database name or use the overload that takes a database name as a parameter.
at Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions.AddCosmosDbContext[TContext](IHostApplicationBuilder builder, String connectionName, Action`1 configureSettings, Action`1 configureDbContextOptions) in /_/src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos/AspireAzureEFCoreCosmosExtensions.cs:line 55
at Program.$(String[] args)
```
### .NET Version info
.NET SDK:
Version: 9.0.204
Commit: 23062858b9
Workload version: 9.0.200-manifests.5591ce44
MSBuild version: 17.13.25+b1feb5ea6
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.204\
### Anything else?
Package versions:
```xml
```
IDE info:
JetBrains Rider 2025.1.2
Build #RD-251.25410.119, built on May 7, 2025
Source revision: d31f38dce07eb
.NET Aspire plugin 1.7.10
Contributor guide
Assessment
This issue has not been assessed yet.