dotnet/efcore · error · NotSupportedException

The Cosmos database does not support 'CanConnect' or…

Error message

The Cosmos database does not support 'CanConnect' or 'CanConnectAsync'.

What it means

The Cosmos DB provider does not implement a connectivity check, so CanConnectAsync (and the sync CanConnect) always throw NotSupportedException. Cosmos has no lightweight 'can I connect?' probe equivalent to relational providers' SELECT 1, and the provider authors chose to fail loudly rather than pretend.

Solutions

  1. Replace the EF CanConnect call with a direct Cosmos SDK probe, e.g. await cosmosClient.GetDatabase(dbId).ReadAsync().
  2. Use a custom IHealthCheck that calls the Cosmos SDK ReadDatabaseAsync and reports Healthy/Unhealthy.
  3. Remove the CanConnect-based liveness check for Cosmos contexts entirely.

Example fix

// before
public Task<bool> CheckAsync(DbContext db)
    => db.Database.CanConnectAsync();

// after - probe Cosmos directly
public async Task<bool> CheckAsync(CosmosClient client, string dbId)
{
    try { await client.GetDatabase(dbId).ReadAsync(); return true; }
    catch (CosmosException) { return false; }
}
Defensive patterns

Strategy: validation

Validate before calling

// Guard any health-check against Cosmos before invoking CanConnect
if (dbContext.Database.ProviderName == "Microsoft.EntityFrameworkCore.Cosmos")
{
    // probe Cosmos directly instead of CanConnect/CanConnectAsync
    return await cosmosClient.GetDatabase(dbId).ReadAsync(cancellationToken: ct) is not null;
}
return await dbContext.Database.CanConnectAsync();

Prevention

When it happens

Trigger: Calling dbContext.Database.CanConnectAsync() or CanConnect() on a Cosmos-backed DbContext. Commonly invoked by ASP.NET Core health checks (AddDbContextCheck) or container orchestrators that probe DB liveness.

Common situations: Adding a standard EF Core health check to a Cosmos-using service. Reusing a relational health-check pattern on a Cosmos context. CI smoke tests that call CanConnect.

Related errors


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

Appendix: source

Thrown at src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs:278

    }

    /// <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 Task<bool> EnsureDeletedAsync(CancellationToken cancellationToken = default)
        => _cosmosClient.DeleteDatabaseAsync(cancellationToken);

    /// <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 Task<bool> CanConnectAsync(CancellationToken cancellationToken = default)
        => throw new NotSupportedException(CosmosStrings.CanConnectNotSupported);

    /// <summary>
    ///     Returns the store names of the properties that is used to store the partition keys.
    /// </summary>
    /// <remarks>
    ///     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.
    /// </remarks>
    /// <param name="entityType">The entity type to get the partition key property names for.</param>
    /// <returns>The names of the partition key property.</returns>
    private static IReadOnlyList<string> GetPartitionKeyStoreNames(IEntityType entityType)
    {
        var properties = entityType.GetPartitionKeyProperties();
        return properties.Any()
            ? properties.Select(p => p.GetJsonPropertyName()).ToList()
            : [CosmosClientWrapper.DefaultPartitionKey];

View on GitHub (pinned to 3a2006ef56)