dotnet/efcore · error · NotSupportedException

This enumerator cannot be reset.

Error message

This enumerator cannot be reset.

What it means

Thrown from Enumerator.Reset() on SingleQueryingEnumerable (SingleQueryingEnumerable.cs:296), the enumerator behind a normal (non-grouped, non-split) relational query. It wraps a forward-only DbDataReader and cannot be rewound, so Reset is unsupported by design.

Source

Thrown at src/EFCore.Relational/Query/Internal/SingleQueryingEnumerable.cs:296

            enumerator._relationalQueryContext.InitializeStateManager(enumerator._standAloneStateManager);

            return false;
        }

        public void Dispose()
        {
            if (_dataReader is not null)
            {
                _relationalQueryContext.Connection.ReturnCommand(_relationalCommand!);
                _dataReader.Dispose();
                _dataReader = null;
                _dbDataReader = null;
            }
        }

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

    private sealed class AsyncEnumerator : IAsyncEnumerator<T>
    {
        private readonly RelationalQueryContext _relationalQueryContext;
        private readonly RelationalCommandResolver _relationalCommandResolver;
        private readonly IReadOnlyList<ReaderColumn?>? _readerColumns;
        private readonly Func<QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator, T> _shaper;
        private readonly Type _contextType;
        private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
        private readonly bool _standAloneStateManager;
        private readonly bool _detailedErrorsEnabled;
        private readonly IConcurrencyDetector? _concurrencyDetector;
        private readonly IExceptionDetector _exceptionDetector;
        private readonly CancellationToken _cancellationToken;

        private IRelationalCommand? _relationalCommand;
        private RelationalDataReader? _dataReader;

View on GitHub (pinned to dbf9771522)

Solutions

  1. Materialize with ToList()/ToListAsync() and iterate the list (List<T>.Enumerator does not throw on Reset and is freely re-iterable via foreach).
  2. Re-run the IQueryable for a fresh enumerator (re-issues SQL).
  3. Avoid manual Reset; use foreach/await foreach.
  4. If a component requires resettable enumeration, pass the buffered list.

Example fix

// before
using var e = context.Blogs.GetEnumerator();
e.MoveNext();
e.Reset(); // NotSupportedException

// after
var list = context.Blogs.ToList();
foreach (var b in list) { /* iterate as often as needed */ }
Defensive patterns

Strategy: validation

Validate before calling

var list = ctx.Set<T>().ToList(); // buffer; iterate the list freely

Type guard

static bool IsResettable<T>(IEnumerable<T> s) => s is IList<T> or Array;

Try / catch

try { e.Reset(); }
catch (NotSupportedException) { var buffered = query.ToList(); }

Prevention

When it happens

Trigger: Calling IEnumerator.Reset() directly on an enumerator obtained from a standard EF LINQ query. Triggered by manual enumerator reuse or libraries that assume Reset works.

Common situations: Hand-written enumeration loops that call Reset; third-party helpers; porting LINQ-to-Objects code that relied on Reset; caching an enumerator for reuse.

Related errors


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