dotnet/efcore · error · NotSupportedException

This enumerator cannot be reset.

Error message

This enumerator cannot be reset.

What it means

Thrown when calling Reset() on the IEnumerator<T> returned by a FromSql query enumerable. EF Core's query enumerators are forward-only and do not support resetting; they hold a single DbDataReader that cannot be rewound. This is by design — IEnumerator.Reset is conventionally allowed to throw NotSupportedException.

Solutions

  1. Do not call Reset(); instead, re-execute the query by calling FromSqlRaw/FromSqlInterpolated again to get a fresh enumerable.
  2. Materialize results with ToList()/ToListAsync() before passing them to code that may call Reset().
  3. If a library calls Reset() internally, report the issue to that library — EF Core enumerators are legitimately non-resettable.

Example fix

// before
using var enum = context.Blogs.FromSqlRaw("SELECT * FROM Blogs").GetEnumerator();
enum.MoveNext();
enum.Reset(); // throws

// after
var blogs = context.Blogs.FromSqlRaw("SELECT * FROM Blogs").ToList();
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Calling .Reset() on the enumerator obtained from a FromSqlRaw/FromSqlInterpolated query's GetEnumerator(). This is almost always done by a third-party library or framework that naively calls Reset() on any IEnumerator, rather than by user code directly.

Common situations: Using a third-party collection wrapper or testing framework that invokes IEnumerator.Reset(). Attempting to enumerate the same FromSql query result twice via the same enumerator instead of re-executing the query.

Related errors


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

Appendix: source

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

            enumerator._indexMap = BuildIndexMap(enumerator._columnNames, enumerator._dataReader.DbDataReader);

            enumerator._relationalQueryContext.InitializeStateManager(enumerator._standAloneStateManager);

            return false;
        }

        public void Dispose()
        {
            if (_dataReader is not null)
            {
                _relationalQueryContext.Connection.ReturnCommand(_relationalCommand!);
                _dataReader?.Dispose();
                _dataReader = 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 IReadOnlyList<string> _columnNames;
        private readonly Func<QueryContext, DbDataReader, int[], 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 IRelationalCommand? _relationalCommand;
        private RelationalDataReader? _dataReader;

View on GitHub (pinned to 3a2006ef56)