{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs#L260-L296","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Replace the EF CanConnect call with a direct Cosmos SDK probe, e.g. await cosmosClient.GetDatabase(dbId).ReadAsync().","Use a custom IHealthCheck that calls the Cosmos SDK ReadDatabaseAsync and reports Healthy/Unhealthy.","Remove the CanConnect-based liveness check for Cosmos contexts entirely."],"exampleFix":"// before\npublic Task<bool> CheckAsync(DbContext db)\n    => db.Database.CanConnectAsync();\n\n// after - probe Cosmos directly\npublic async Task<bool> CheckAsync(CosmosClient client, string dbId)\n{\n    try { await client.GetDatabase(dbId).ReadAsync(); return true; }\n    catch (CosmosException) { return false; }\n}","handlingStrategy":"validation","validationCode":"// Guard any health-check against Cosmos before invoking CanConnect\nif (dbContext.Database.ProviderName == \"Microsoft.EntityFrameworkCore.Cosmos\")\n{\n    // probe Cosmos directly instead of CanConnect/CanConnectAsync\n    return await cosmosClient.GetDatabase(dbId).ReadAsync(cancellationToken: ct) is not null;\n}\nreturn await dbContext.Database.CanConnectAsync();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not call CanConnect/CanConnectAsync on Cosmos contexts.","Use the Cosmos SDK's ReadDatabaseAsync as the liveness probe.","Add a unit test asserting your health check skips EF's CanConnect for Cosmos."],"tags":["cosmos","canconnect","health-check","notsupported"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}