{"record":{"id":"e26574f5061c5e97","repo":"dotnet/efcore","slug":"azure-cosmos-db-does-not-support-synchronous-i-o-e26574","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/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs","lineNumber":62,"sourceCode":"            _rootEntityType = rootEntityType;\n            _readItemInfo = readItemInfo;\n            _shaper = shaper;\n            _contextType = contextType;\n            _queryLogger = _cosmosQueryContext.QueryLogger;\n            _standAloneStateManager = standAloneStateManager;\n            _threadSafetyChecksEnabled = threadSafetyChecksEnabled;\n\n            _cosmosContainer = rootEntityType.GetContainer()\n                ?? throw new UnreachableException(\"Root entity type without a Cosmos container.\");\n            _cosmosPartitionKey = GeneratePartitionKey(\n                rootEntityType, partitionKeyPropertyValues, _cosmosQueryContext.Parameters);\n        }\n\n        public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)\n            => new AsyncEnumerator(this, cancellationToken);\n\n        public IEnumerator<T> GetEnumerator()\n            => throw new InvalidOperationException(CosmosStrings.SyncNotSupported);\n\n        IEnumerator IEnumerable.GetEnumerator()\n            => GetEnumerator();\n\n        public string ToQueryString()\n        {\n            TryGetResourceId(out var resourceId);\n            return CosmosStrings.NoReadItemQueryString(resourceId, _cosmosPartitionKey);\n        }\n\n        private bool TryGetResourceId(out string resourceId)\n        {\n            var jsonIdDefinition = _rootEntityType.GetJsonIdDefinition();\n            Check.DebugAssert(\n                jsonIdDefinition != null,\n                \"Should not be using this enumerable if not using ReadItem, which needs an id definition.\");\n\n            var values = new List<object>(jsonIdDefinition.Properties.Count);","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs#L44-L80","documentation":"Identical to the standard Cosmos sync-not-supported guard, but thrown specifically by the ReadItem-query path (ReadItemQueryingEnumerable.GetEnumerator). The ReadItem optimization performs a point read via the Cosmos SDK, which is async-only, so synchronous enumeration is blocked.","triggerScenarios":"Triggering a point-read (ReadItem) optimized query and then enumerating it synchronously (ToList, First, foreach). ReadItem optimization kicks in for PK lookups like Where(b => b.Id == id) combined with partition key.","commonSituations":"A query that the provider optimizes to a ReadItem (single document point read) being consumed via sync LINQ. Migrating a relational GetById that used First() synchronously.","solutions":["Use async APIs: FirstAsync, SingleAsync, ToListAsync on the query.","Ensure the consuming layer is async all the way up.","Verify no synchronous helper enumerates the result before returning to an async context."],"exampleFix":"// before\nvar blog = context.Blogs.WithPartitionKey(pk).Where(b => b.Id == id).First();\n// after\nvar blog = await context.Blogs.WithPartitionKey(pk).Where(b => b.Id == id).FirstAsync();","handlingStrategy":"validation","validationCode":"// Use async point-read APIs only\npublic async Task<Blog?> FindAsync(string pk, string id)\n    => await context.Blogs.WithPartitionKey(pk).Where(b => b.Id == id).FirstOrDefaultAsync();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use FirstAsync/SingleAsync for point-read queries on Cosmos.","Audit repository methods to ensure none call sync LINQ on Cosmos queries.","Enable blocking-sync detection (configure context options) in development."],"tags":["cosmos","sync-io","async","read-item","query"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}