dotnet/efcore · error · InvalidOperationException

Azure Cosmos DB does not support synchronous I/O. Make sure

Error message

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.

What it means

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.

Source

Thrown at src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs:65

            _querySqlGeneratorFactory = querySqlGeneratorFactory;
            _selectExpression = selectExpression;
            _shaper = shaper;
            _contextType = contextType;
            _queryLogger = cosmosQueryContext.QueryLogger;
            _standAloneStateManager = standAloneStateManager;
            _threadSafetyChecksEnabled = threadSafetyChecksEnabled;

            _cosmosContainer = rootEntityType.GetContainer()
                ?? throw new UnreachableException("Root entity type without a Cosmos container.");
            _cosmosPartitionKey = GeneratePartitionKey(
                rootEntityType, partitionKeyPropertyValues, _cosmosQueryContext.Parameters);
        }

        public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
            => new AsyncEnumerator(this, cancellationToken);

        public IEnumerator<T> GetEnumerator()
            => throw new InvalidOperationException(CosmosStrings.SyncNotSupported);

        IEnumerator IEnumerable.GetEnumerator()
            => GetEnumerator();

        private CosmosSqlQuery GenerateQuery()
            => _querySqlGeneratorFactory.Create().GetSqlQuery(
                (SelectExpression)new ParameterInliner(
                        _sqlExpressionFactory,
                        _cosmosQueryContext.Parameters)
                    .Visit(_selectExpression),
                _cosmosQueryContext.Parameters!);

        public string ToQueryString()
        {
            var sqlQuery = GenerateQuery();
            if (sqlQuery.Parameters.Count == 0)
            {
                return sqlQuery.Query;

View on GitHub (pinned to dbf9771522)

Solutions

  1. Use the async equivalents: ToListAsync(), FirstAsync(), CountAsync(), etc.
  2. Await the query: await foreach (var item in context.Blogs.AsAsyncEnumerable()).
  3. Audit for sync-only enumeration paths (e.g. AutoMapper synchronous projections) and replace with async versions.

Example fix

// before
var blogs = context.Blogs.Where(b => b.Active).ToList();
// after
var blogs = await context.Blogs.Where(b => b.Active).ToListAsync();
Defensive patterns

Strategy: validation

Validate before calling

// Enforce async usage at compile time by returning IAsyncEnumerable / Task
public async Task<List<Blog>> GetAsync()
    => await context.Blogs.Where(b => b.Active).ToListAsync();

Prevention

When it happens

Trigger: 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.

Common situations: Porting relational code that used sync LINQ. Using third-party libraries or helpers that enumerate IQueryable synchronously. Forgetting the Async suffix.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/3eee38874321d7ab. Report an issue: GitHub.