{"record":{"id":"83ee1cd105a1905b","repo":"dotnet/efcore","slug":"azure-cosmos-db-does-not-support-synchronous-i-o-83ee1c","errorCode":null,"errorMessage":"Azure Cosmos DB does not support synchronous I/O. Make sure to use and correctly await only async methods when using Entity Framework Core to access Azure Cosmos DB.","messagePattern":"Azure Cosmos DB does not support synchronous I/O\\. Make sure to use and correctly await only async methods when using Entity Framework Core to access Azure Cosmos DB\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs","lineNumber":308,"sourceCode":"    /// <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];\n    }\n\n    #region Unsupported sync methods\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 bool EnsureCreated()\n        => throw new InvalidOperationException(CosmosStrings.SyncNotSupported);\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 bool EnsureDeleted()\n        => throw new InvalidOperationException(CosmosStrings.SyncNotSupported);\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 bool CanConnect()\n        => throw new InvalidOperationException(CosmosStrings.SyncNotSupported);","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs#L290-L326","documentation":"The Cosmos DB provider only supports asynchronous I/O because the underlying Microsoft.Azure.Cosmos SDK has no synchronous API surface. The synchronous EnsureCreated() method (CosmosDatabaseCreator.cs:307-308) unconditionally throws InvalidOperationException to prevent deadlocks and force correct async usage. The provider's entire storage layer is async-only.","triggerScenarios":"Calling context.Database.EnsureCreated() (no await, no Async suffix) on a Cosmos-backed DbContext. This is the synchronous overload that directly throws before any work is done.","commonSituations":"Copy-pasting relational code that calls EnsureCreated() synchronously. Calling from a non-async context (e.g., a property getter, a static initializer, Main without async). Forgetting the Async suffix.","solutions":["Use the async overload: await context.Database.EnsureCreatedAsync().","Make the calling method async all the way up; do not use .Result or .Wait() which can deadlock.","If trapped in a sync context, use a top-level async Main (static async Task Main) or an async overload of the host lifecycle method."],"exampleFix":"// before\ncontext.Database.EnsureCreated();\n\n// after\nawait context.Database.EnsureCreatedAsync();","handlingStrategy":"validation","validationCode":"// Ensure EnsureCreatedAsync is used. There is no runtime guard; enforce via code review and async APIs.\n// Quick check: if a Cosmos provider is detected, all DB-management calls must be async.\nvar provider = context.Database.ProviderName;\nif (provider == \"Microsoft.EntityFrameworkCore.Cosmos\")\n{\n    // never call EnsureCreated(); always await EnsureCreatedAsync()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always use EnsureCreatedAsync() with the Cosmos provider.","Make startup/initialization methods async all the way up.","Run a Roslyn analyzer or code-review check for sync DB-management calls on Cosmos contexts."],"tags":["cosmos","sync-not-supported","async","database-management"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}