dotnet/efcore · critical · InvalidOperationException

None of connection string, CredentialToken, account key or a

Error message

None of connection string, CredentialToken, account key or account endpoint were specified. Specify a set of connection details.

What it means

Thrown in the SingletonCosmosClientWrapper constructor when none of the Cosmos connection inputs are supplied. The constructor pattern-matches on options: ConnectionString first, then TokenCredential (+AccountEndpoint), then AccountEndpoint (+AccountKey); if all are null/empty it throws because there is no way to construct a CosmosClient. At least one valid connection detail set is mandatory for the Cosmos provider.

Source

Thrown at src/EFCore.Cosmos/Storage/Internal/SingletonCosmosClientWrapper.cs:102

        if (options.HttpClientFactory != null)
        {
            configuration.HttpClientFactory = options.HttpClientFactory;
        }

        if (options.EnableBulkExecution != null)
        {
            configuration.AllowBulkExecution = options.EnableBulkExecution.Value;
        }

        configuration.EnableContentResponseOnWrite = options.EnableContentResponseOnWrite == true;

        _client = options switch
        {
            { ConnectionString: not null and not "" } => new CosmosClient(options.ConnectionString, configuration),
            { TokenCredential: not null } => new CosmosClient(options.AccountEndpoint, options.TokenCredential, configuration),
            { AccountEndpoint: not null } => new CosmosClient(options.AccountEndpoint, options.AccountKey, configuration),
            _ => throw new InvalidOperationException(CosmosStrings.ConnectionInfoMissing)
        };
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual CosmosClient Client
        => _client;

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>

View on GitHub (pinned to dbf9771522)

Solutions

  1. Provide a non-empty connection string to UseCosmos, e.g. optionsBuilder.UseCosmos(connectionString, databaseName).
  2. Or provide accountEndpoint + accountKey, or accountEndpoint + tokenCredential.
  3. Validate at startup that the connection string is non-empty before building the DbContext (it may be bound from a missing config key).

Example fix

// before - connection string from config is null/empty
var cosmosSection = config.GetSection("WrongKey");
optionsBuilder.UseCosmos(cosmosSection["ConnectionString"], "MyDb"); // throws

// after - bind the correct non-empty value
var connStr = config.GetConnectionString("Cosmos")
    ?? throw new InvalidOperationException("Cosmos connection string missing");
optionsBuilder.UseCosmos(connStr, "MyDb");
Defensive patterns

Strategy: validation

Validate before calling

var connStr = config.GetConnectionString("Cosmos");
if (string.IsNullOrWhiteSpace(connStr))
    throw new InvalidOperationException("Cosmos connection string is missing in configuration.");
optionsBuilder.UseCosmos(connStr, databaseName);

Prevention

When it happens

Trigger: Calling UseCosmos with a database name but omitting the connection string, account endpoint, account key, and token credential. Or passing an empty string for ConnectionString (empty is treated as null by the pattern guard).

Common situations: Relying on a configuration section that is missing or misnamed (e.g., binding the wrong key). Environment-specific config where the Cosmos connection string is injected only in Production but the app runs in Development without it. Using Azure RBAC/TokenCredential but forgetting to also set AccountEndpoint. Empty-string connection strings from IConfiguration that look non-null but are treated as absent.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/6fb16c71bbadbf6c. Report an issue: GitHub.