{"record":{"id":"bf62064123587e5d","repo":"dotnet/efcore","slug":"this-enumerator-cannot-be-reset-bf6206","errorCode":null,"errorMessage":"This enumerator cannot be reset.","messagePattern":"This enumerator cannot be reset\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/Internal/SingleQueryingEnumerable.cs","lineNumber":296,"sourceCode":"\n            enumerator._relationalQueryContext.InitializeStateManager(enumerator._standAloneStateManager);\n\n            return false;\n        }\n\n        public void Dispose()\n        {\n            if (_dataReader is not null)\n            {\n                _relationalQueryContext.Connection.ReturnCommand(_relationalCommand!);\n                _dataReader.Dispose();\n                _dataReader = null;\n                _dbDataReader = null;\n            }\n        }\n\n        public void Reset()\n            => throw new NotSupportedException(CoreStrings.EnumerableResetNotSupported);\n    }\n\n    private sealed class AsyncEnumerator : IAsyncEnumerator<T>\n    {\n        private readonly RelationalQueryContext _relationalQueryContext;\n        private readonly RelationalCommandResolver _relationalCommandResolver;\n        private readonly IReadOnlyList<ReaderColumn?>? _readerColumns;\n        private readonly Func<QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator, T> _shaper;\n        private readonly Type _contextType;\n        private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;\n        private readonly bool _standAloneStateManager;\n        private readonly bool _detailedErrorsEnabled;\n        private readonly IConcurrencyDetector? _concurrencyDetector;\n        private readonly IExceptionDetector _exceptionDetector;\n        private readonly CancellationToken _cancellationToken;\n\n        private IRelationalCommand? _relationalCommand;\n        private RelationalDataReader? _dataReader;","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/Internal/SingleQueryingEnumerable.cs#L278-L314","documentation":"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.","triggerScenarios":"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.","commonSituations":"Hand-written enumeration loops that call Reset; third-party helpers; porting LINQ-to-Objects code that relied on Reset; caching an enumerator for reuse.","solutions":["Materialize with ToList()/ToListAsync() and iterate the list (List<T>.Enumerator does not throw on Reset and is freely re-iterable via foreach).","Re-run the IQueryable for a fresh enumerator (re-issues SQL).","Avoid manual Reset; use foreach/await foreach.","If a component requires resettable enumeration, pass the buffered list."],"exampleFix":"// before\nusing var e = context.Blogs.GetEnumerator();\ne.MoveNext();\ne.Reset(); // NotSupportedException\n\n// after\nvar list = context.Blogs.ToList();\nforeach (var b in list) { /* iterate as often as needed */ }","handlingStrategy":"validation","validationCode":"var list = ctx.Set<T>().ToList(); // buffer; iterate the list freely","typeGuard":"static bool IsResettable<T>(IEnumerable<T> s) => s is IList<T> or Array;","tryCatchPattern":"try { e.Reset(); }\ncatch (NotSupportedException) { var buffered = query.ToList(); }","preventionTips":["Never call Reset() on EF query enumerators; materialize first.","Use foreach over ToList() results.","Pass buffered lists to libraries requiring resettable enumerators."],"tags":["ef-core","enumerator","reset","datareader","query"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}