dotnet/efcore · critical · InvalidOperationException

None of connection string, CredentialToken, account key or…

Error message

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

What it means

Thrown by SingletonCosmosClientWrapper when constructing the CosmosClient at startup: none of ConnectionString, TokenCredential (+ AccountEndpoint), or AccountEndpoint (+ AccountKey) were configured on CosmosDbContextOptionsExtensions. The switch expression has no matching arm, so the provider cannot know which Cosmos account to contact. It is an InvalidOperationException surfaced during the first use of the Cosmos context.

Solutions

  1. Pass a full connection string to UseCosmos: options.UseCosmos("AccountEndpoint=https://...;AccountKey=...;", dbName).
  2. Or set AccountEndpoint plus either TokenCredential or AccountKey via the Cosmos options extension before model finalization.
  3. Verify the configuration section is bound: builder.Configuration.GetSection("Cosmos").Exists() and that the connection string is non-empty.
  4. If loading from secrets/environment variables, confirm the variable name matches the one referenced in AddCosmos/UseCosmos.

Example fix

// before
services.AddDbContext<MyCtx>(o => o.UseCosmos(accountName: "myaccount", dbName));

// after
services.AddDbContext<MyCtx>(o =>
    o.UseCosmos(
        "AccountEndpoint=https://myaccount.documents.azure.com:443/;AccountKey=<key>;",
        dbName));
Defensive patterns

Strategy: validation

Validate before calling

var cosmos = configuration.GetSection("Cosmos");
if (string.IsNullOrWhiteSpace(cosmos["ConnectionString"])
    && string.IsNullOrWhiteSpace(cosmos["AccountEndpoint"]))
{
    throw new InvalidOperationException(
        "Cosmos requires ConnectionString or AccountEndpoint (+ AccountKey/TokenCredential).");
}

services.AddDbContext<Ctx>(o => o.UseCosmos(cosmos["ConnectionString"]!, cosmos["Database"]!));

Type guard

static bool HasCosmosConnection(CosmosDbContextOptionsBuilder o)
    => !string.IsNullOrWhiteSpace(o.OptionsExtension.ConnectionString)
       || o.OptionsExtension.AccountEndpoint is not null;

Prevention

When it happens

Trigger: Calling UseCosmos() without any connection argument, or passing only a region/account name while leaving the actual endpoint, key, token, or full connection string empty. Also triggered when reading these values from configuration that resolves to null (e.g. a missing 'Cosmos:ConnectionString' key).

Common situations: Misnamed appsettings key, connection string moved to a different section after a config refactor, running design-time tools (dotnet ef migrations) in an environment where the Cosmos section is not bound, or migrating from account key auth to TokenCredential and forgetting to set AccountEndpoint.

Related errors


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

Appendix: 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 3a2006ef56)