{"record":{"id":"3eee38874321d7ab","repo":"dotnet/efcore","slug":"azure-cosmos-db-does-not-support-synchronous-i-o","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.QueryingEnumerable.cs","lineNumber":65,"sourceCode":"            _querySqlGeneratorFactory = querySqlGeneratorFactory;\n            _selectExpression = selectExpression;\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        private CosmosSqlQuery GenerateQuery()\n            => _querySqlGeneratorFactory.Create().GetSqlQuery(\n                (SelectExpression)new ParameterInliner(\n                        _sqlExpressionFactory,\n                        _cosmosQueryContext.Parameters)\n                    .Visit(_selectExpression),\n                _cosmosQueryContext.Parameters!);\n\n        public string ToQueryString()\n        {\n            var sqlQuery = GenerateQuery();\n            if (sqlQuery.Parameters.Count == 0)\n            {\n                return sqlQuery.Query;","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs#L47-L83","documentation":"Cosmos DB SDK only exposes async APIs (ExecuteItemQueryAsync, ReadItemAsync, etc.). The generated QueryingEnumerable explicitly implements IEnumerator<T> GetEnumerator to throw InvalidOperationException so that any synchronous enumeration (foreach without await, .ToList(), .First()) of a Cosmos query fails fast rather than deadlocking.","triggerScenarios":"Calling a synchronous enumeration API on a Cosmos-backed DbSet: ToList(), First(), Single(), Count(), foreach over the IQueryable, or any LINQ extension that pulls IEnumerator<T>.","commonSituations":"Copy-pasting relational EF code that uses ToList() into a Cosmos project, legacy sync service code being pointed at Cosmos, or third-party libraries that enumerate synchronously.","solutions":["Use the async equivalents everywhere: ToListAsync, FirstAsync, CountAsync, await foreach with IAsyncEnumerable.","Audit for stray synchronous LINQ operators over Cosmos DbSets.","Configure analyzers to flag sync enumeration on IQueryable."],"exampleFix":"// before\nvar users = db.Users.ToList();\n// after\nvar users = await db.Users.ToListAsync();","handlingStrategy":"validation","validationCode":"// enable CA1849 / analyzers that flag sync-over-async on IQueryable\n// in csproj:\n// <PackageReference Include=\"Microsoft.CodeAnalysis.NetAnalyzers\" />\n// and treat CA1849 as error","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ban sync LINQ extensions on DbSets via editorconfig/analyzer rules.","Expose IAsyncEnumerable<T> from repositories, not IQueryable<T>.","Code-review any ToList/First/Count added against a Cosmos context."],"tags":["cosmos","async","enumeration","io"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}