{"record":{"id":"ee1268be74e07a3b","repo":"dotnet/efcore","slug":"the-cosmos-database-does-not-support-canconnect","errorCode":null,"errorMessage":"The Cosmos database does not support 'CanConnect' or 'CanConnectAsync'.","messagePattern":"The Cosmos database does not support 'CanConnect' or 'CanConnectAsync'\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs","lineNumber":278,"sourceCode":"    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual Task<bool> EnsureDeletedAsync(CancellationToken cancellationToken = default)\n        => _cosmosClient.DeleteDatabaseAsync(cancellationToken);\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual Task<bool> CanConnectAsync(CancellationToken cancellationToken = default)\n        => throw new NotSupportedException(CosmosStrings.CanConnectNotSupported);\n\n    /// <summary>\n    ///     Returns the store names of the properties that is used to store the partition keys.\n    /// </summary>\n    /// <remarks>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </remarks>\n    /// <param name=\"entityType\">The entity type to get the partition key property names for.</param>\n    /// <returns>The names of the partition key property.</returns>\n    private static IReadOnlyList<string> GetPartitionKeyStoreNames(IEntityType entityType)\n    {\n        var properties = entityType.GetPartitionKeyProperties();\n        return properties.Any()\n            ? properties.Select(p => p.GetJsonPropertyName()).ToList()\n            : [CosmosClientWrapper.DefaultPartitionKey];","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs#L260-L296","documentation":"The Cosmos DB provider does not implement health-check style connectivity verification. CanConnectAsync (CosmosDatabaseCreator.cs:277-278) unconditionally throws NotSupportedException because Cosmos does not expose a lightweight 'can I reach the database?' probe that EF Core can call without side effects. The sync CanConnect variant throws SyncNotSupported instead (see error 131).","triggerScenarios":"Calling context.Database.CanConnectAsync() or context.GetDatabaseCreator().CanConnectAsync() on a DbContext configured with UseCosmos. Also triggered by ASP.NET Core health check infrastructure that invokes CanConnect when EFCore is registered as a health check.","commonSituations":"Adding .AddDbContextCheck<MyContext>() to an ASP.NET Core health check pipeline with a Cosmos-backed context. Calling CanConnect in startup to verify connectivity. Using a shared health-check library that probes all registered DbContexts.","solutions":["Do not use EF Core's CanConnect for Cosmos health checks. Instead, use the Cosmos SDK's Microsoft.Azure.Cosmos.Database.ReadAsync or a custom IHealthCheck that pings the Cosmos client.","Exclude Cosmos-backed DbContexts from .AddDbContextCheck and register a dedicated Cosmos health check (e.g., fromAspNetCore.Diagnostics.HealthChecks or a custom implementation).","If you must use a connectivity probe, wrap a raw Cosmos SDK call (client.GetDatabase(name).ReadStreamAsync()) in a try/catch."],"exampleFix":"// before\nservices.AddHealthChecks().AddDbContextCheck<CosmosDbContext>();\n\n// after\nservices.AddHealthChecks().AddAzureCosmosDB(\n    cosmosClientFactory: sp => sp.GetRequiredService<CosmosClient>(),\n    databaseName: \"MyDb\");","handlingStrategy":"fallback","validationCode":"// Do not call CanConnect on Cosmos. Use a Cosmos SDK probe instead.\nstatic async Task<bool> CanConnectCosmosAsync(CosmosClient client, string dbName, CancellationToken ct = default)\n{\n    try { await client.GetDatabase(dbName).ReadAsync(cancellationToken: ct); return true; }\n    catch (CosmosException) { return false; }\n}","typeGuard":null,"tryCatchPattern":"// If third-party code calls CanConnectAsync, catch NotSupportedException and fall back to a Cosmos SDK probe.\ntry { await context.Database.CanConnectAsync(); }\ncatch (NotSupportedException) { /* use Cosmos SDK health check instead */ }","preventionTips":["Never register Cosmos DbContexts with AddDbContextCheck.","Use a dedicated Cosmos health check (e.g., AddAzureCosmosDB) for health endpoints.","Document in your team that CanConnect is unsupported for Cosmos."],"tags":["cosmos","health-check","not-supported","connectivity"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}