dotnet/efcore · error · NotSupportedException

This enumerator cannot be reset.

Error message

This enumerator cannot be reset.

What it means

The AsyncEnumerator implements IEnumerator/IEnumerator<T>.Reset explicitly and throws NotSupportedException (CoreStrings.EnumerableResetNotSupported) — EF's async enumerators are forward-only because Cosmos streams cannot rewind. Compiler-generated foreach reset logic or explicit Reset() calls trip this.

Solutions

  1. Re-execute the query to 'reset' — do not reuse the enumerator.
  2. Materialize results into a List<T> with ToListAsync if you need multiple enumerations.
  3. Avoid APIs that contractually require IEnumerator.Reset.

Example fix

// before
var e = ((IEnumerable<User>)query).GetEnumerator();
while (e.MoveNext()) { /*...*/ }
e.Reset();
// after
var list = await query.ToListAsync();
foreach (var u in list) { /*...*/ }
Defensive patterns

Strategy: try-catch

Validate before calling

static async Task<List<T>> MaterializeAsync<T>(IQueryable<T> q, CancellationToken ct)
    where T : class
    => await q.ToListAsync(ct).ConfigureAwait(false);  // never reuse enumerators

Try / catch

try
{
    enumerator.Reset();
}
catch (NotSupportedException ex) when (ex.Message.Contains("cannot be reset"))
{
    // re-issue the query instead
}

Prevention

When it happens

Trigger: Calling .Reset() on the enumerator obtained from a Cosmos query, or using an API that internally resets enumerators (some grid/binding components, .NET's Enumerable.Take on a reset-capable wrapper).

Common situations: UI data binding that re-enumerates the same source, third-party batching helpers that call Reset between passes, or migrating from List-backed sources to IQueryable.

Related errors


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

Appendix: 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 3a2006ef56)