{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs#L290-L326","documentation":"EnsureCreated() is the synchronous database-creation entry point on CosmosDatabaseCreator. Because the Azure Cosmos DB SDK only exposes async APIs, the provider deliberately throws InvalidOperationException for all synchronous store-management methods. You must call the async equivalent.","triggerScenarios":"Calling dbContext.Database.EnsureCreated() (sync) on a Cosmos provider. Triggered by scaffolding code, synchronous startup paths, or test fixtures that use the sync API.","commonSituations":"Porting a relational app where EnsureCreated() worked. A synchronous Main or a non-async test setup calling EnsureCreated(). Result.Wait()/.GetAwaiter().GetResult() wrappers around the async call hit this through a different overload.","solutions":["Call await dbContext.Database.EnsureCreatedAsync(cancellationToken) instead.","If blocked by a non-async caller, make the caller async (e.g. public static async Task Main).","For tests, use the async overloads provided by the test fixture."],"exampleFix":"// before\ndbContext.Database.EnsureCreated();\n\n// after\nawait dbContext.Database.EnsureCreatedAsync();","handlingStrategy":"validation","validationCode":"// Reject sync creation on Cosmos at the call site\nif (dbContext.Database.ProviderName == \"Microsoft.EntityFrameworkCore.Cosmos\")\n{\n    throw new InvalidOperationException(\"Use EnsureCreatedAsync for Cosmos.\");\n}\ndbContext.Database.EnsureCreated();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always use EnsureCreatedAsync for Cosmos contexts.","Mark startup paths async from the entry point down.","Lint your codebase for EnsureCreated() on Cosmos projects."],"tags":["cosmos","syncnotsupported","ensurecreated","async"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}