{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs#L44-L80","documentation":"Same SyncNotSupported guard as the regular QueryingEnumerable, but on the ReadItem (point-lookup) path. When EF optimizes a query to a single ReadItemAsync call, the ReadItemQueryingEnumerable still implements IEnumerator<T> GetEnumerator to throw InvalidOperationException on any synchronous enumeration.","triggerScenarios":"Synchronous enumeration of a query that EF compiled down to a ReadItem (e.g. Find-like lookup by id and partition key), or calling .ToList()/.First() on a query that selects a single entity by its id.","commonSituations":"Wrapping a point-lookup helper that returns IQueryable and the caller enumerates synchronously; reusing sync repository methods against Cosmos.","solutions":["Switch all lookup paths to async: await ...FirstOrDefaultAsync / FindAsync.","Expose IAsyncEnumerable<T> from repository helpers instead of IQueryable<T>.","Static-analyze the codebase for sync LINQ-to-IQueryable call sites."],"exampleFix":"// before\nvar user = db.Users.FirstOrDefault(u => u.Id == id);\n// after\nvar user = await db.Users.FirstOrDefaultAsync(u => u.Id == id);","handlingStrategy":"validation","validationCode":"static async Task<T?> FindAsync<T>(DbSet<T> set, object[] keys, CancellationToken ct)\n    where T : class\n    => await set.FindAsync(keys, ct).AsTask().ConfigureAwait(false);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use FindAsync/FirstOrDefaultAsync for point lookups; never sync over IQueryable.","Wrap lookups in async repository helpers.","Flag sync enumeration call sites with CA1849."],"tags":["cosmos","async","enumeration","readitem","io"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}