{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs#L47-L83","documentation":"Thrown by QueryingEnumerable.GetEnumerator() because the Cosmos SDK only supports asynchronous I/O. EF Core Cosmos deliberately blocks synchronous enumeration to prevent deadlocks and forced blocking on network calls; you must use the async enumeration path (GetAsyncEnumerator) and await it.","triggerScenarios":"Calling any synchronous API that forces enumeration: ToList(), First(), Count(), foreach over the DbSet, or any LINQ method without the Async suffix on a Cosmos query.","commonSituations":"Porting relational code that used sync LINQ. Using third-party libraries or helpers that enumerate IQueryable synchronously. Forgetting the Async suffix.","solutions":["Use the async equivalents: ToListAsync(), FirstAsync(), CountAsync(), etc.","Await the query: await foreach (var item in context.Blogs.AsAsyncEnumerable()).","Audit for sync-only enumeration paths (e.g. AutoMapper synchronous projections) and replace with async versions."],"exampleFix":"// before\nvar blogs = context.Blogs.Where(b => b.Active).ToList();\n// after\nvar blogs = await context.Blogs.Where(b => b.Active).ToListAsync();","handlingStrategy":"validation","validationCode":"// Enforce async usage at compile time by returning IAsyncEnumerable / Task\npublic async Task<List<Blog>> GetAsync()\n    => await context.Blogs.Where(b => b.Active).ToListAsync();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always use the Async-suffixed LINQ extension methods on Cosmos queries.","Never call sync ToList/First/Count on a Cosmos IQueryable.","Make data-access methods async end-to-end to avoid sync over async."],"tags":["cosmos","sync-io","async","query","enumeration"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}