dotnet/efcore · error · NotSupportedException
This enumerator cannot be reset.
Error message
This enumerator cannot be reset.
What it means
Thrown from Enumerator.Reset() on the FromSql query enumerable (FromSqlQueryingEnumerable.cs:296). EF's relational query enumerators wrap a forward-only DbDataReader that streams results from the database; once consumed, the reader cannot be rewound. IEnumerator.Reset is therefore explicitly unsupported and throws NotSupportedException. This is by design: EF enumerables are single-pass and do not buffer the result set.
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 dbf9771522)
Solutions
- Do not call Reset(); instead, materialize results once with ToList()/ToListAsync() and re-iterate the in-memory list.
- Re-execute the query from the IQueryable to get a fresh enumerator (this re-issues the SQL).
- If you need multiple passes, call AsEnumerable() (or AsAsyncEnumerable()) after ToList to move to an in-memory sequence that supports Reset.
- Replace manual enumerator usage with foreach / await foreach.
Example fix
// before - calling Reset on an EF enumerator
var enumerator = context.Blogs.FromSqlRaw("SELECT * FROM Blogs").GetEnumerator();
enumerator.MoveNext();
enumerator.Reset(); // throws NotSupportedException
// after - buffer once, iterate the list freely
var blogs = context.Blogs.FromSqlRaw("SELECT * FROM Blogs").ToList();
var enumerator = blogs.GetEnumerator(); // List<T>.Enumerator supports Reset-free iteration
// or just foreach over the list as many times as needed Defensive patterns
Strategy: validation
Validate before calling
// Never call Reset() on an EF enumerable. Materialize once and iterate the list. var materialized = ctx.Set<T>().FromSqlRaw(sql).ToList(); // List<T> is freely re-iterable
Type guard
// Detect resettable vs single-pass enumerators before calling Reset. static bool IsResettable<T>(IEnumerable<T> source) => source is IList<T> or Array or ICollection<T>; // Usage: if (IsResettable(seq)) e.Reset(); else re-enumerate.
Try / catch
try { e.Reset(); }
catch (NotSupportedException) {
// EF query enumerators cannot be reset; materialize and reuse instead.
var list = query.ToList();
} Prevention
- Avoid calling IEnumerator.Reset() on any EF query; prefer foreach.
- Materialize queries with ToList()/ToListAsync() when you need multiple passes.
- Do not hand EF enumerators to libraries that call Reset; pass the buffered list.
- Document that EF enumerables are single-pass forward-only.
When it happens
Trigger: Code that calls .Reset() on an IEnumerator obtained from an EF query (e.g. via a helper, a LINQ operator implementation, or a library that assumes Reset works). Also triggered by enumerating the same query twice via the same enumerator instance, or by frameworks that call Reset during custom aggregation/caching.
Common situations: Porting code that used LINQ-to-Objects (where Reset is supported) to EF; third-party libraries that buffer/re-iterate enumerators; misuse of GetAsyncEnumerator()/GetEnumerator() directly instead of re-running the query.
Related errors
- This enumerator cannot be reset.
- This enumerator cannot be reset.
- EnumerableResetNotSupported
- This enumerator cannot be reset.
- This enumerator cannot be reset.
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/97b6c82616eeb412.
Report an issue: GitHub.