dotnet/efcore · error · NotSupportedException

This enumerator cannot be reset.

Error message

This enumerator cannot be reset.

What it means

Thrown by Reset() on the ReadItem query enumerator. EF Core's Cosmos enumerators are forward-only, single-pass async enumerators that consume a network response stream; resetting and re-reading is not supported, so Reset throws NotSupportedException.

Source

Thrown at src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs:186

                    else
                    {
                        _queryLogger.QueryIterationFailed(_contextType, exception);
                    }

                    throw;
                }
            }

            public ValueTask DisposeAsync()
            {
                _response = null;
                _hasExecuted = false;

                return default;
            }

            public void Reset()
                => throw new NotSupportedException(CoreStrings.EnumerableResetNotSupported);

            private bool ShapeResult()
            {
                var hasNext = _response is not null;

                _cosmosQueryContext.InitializeStateManager(_standAloneStateManager);

                Current
                    = hasNext
                        ? _shaper(_cosmosQueryContext, _response.Value, ordinal: 0, out _)
                        : default;

                _hasExecuted = true;

                return hasNext;
            }
        }
    }

View on GitHub (pinned to dbf9771522)

Solutions

  1. Do not call Reset on EF Core Cosmos query enumerators; re-execute the query instead.
  2. Cache the materialized result (e.g. ToListAsync) if you need to iterate multiple times.
  3. Avoid enumerator-sharing patterns; consume the async enumerator once.

Example fix

// before
using var e = query.GetAsyncEnumerator();
while (e.MoveNextAsync().Result) { /* ... */ }
e.Reset();
// after
var list = await query.ToListAsync();
foreach (var item in list) { /* iterate as many times as needed */ }
Defensive patterns

Strategy: validation

Validate before calling

// Never reset; materialize once if multiple iterations are needed
var list = await query.ToListAsync();
// iterate list freely; do not call Reset on any enumerator

Prevention

When it happens

Trigger: Calling .Reset() on an IEnumerator obtained from a Cosmos query result. Typically indirect, via a library or LINQ operator that materializes enumerators and calls Reset.

Common situations: Interoperating with code that assumes resettable enumerators (rare). Custom wrappers around EF query results that call Reset. Misuse of enumerator semantics.

Related errors


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