{"record":{"id":"97b6c82616eeb412","repo":"dotnet/efcore","slug":"this-enumerator-cannot-be-reset-97b6c8","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/FromSqlQueryingEnumerable.cs","lineNumber":296,"sourceCode":"            enumerator._indexMap = BuildIndexMap(enumerator._columnNames, enumerator._dataReader.DbDataReader);\n\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            }\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 IReadOnlyList<string> _columnNames;\n        private readonly Func<QueryContext, DbDataReader, int[], 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\n        private IRelationalCommand? _relationalCommand;\n        private RelationalDataReader? _dataReader;","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Query/Internal/FromSqlQueryingEnumerable.cs#L278-L314","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Do not call Reset(); instead, re-execute the query by calling FromSqlRaw/FromSqlInterpolated again to get a fresh enumerable.","Materialize results with ToList()/ToListAsync() before passing them to code that may call Reset().","If a library calls Reset() internally, report the issue to that library — EF Core enumerators are legitimately non-resettable."],"exampleFix":"// before\nusing var enum = context.Blogs.FromSqlRaw(\"SELECT * FROM Blogs\").GetEnumerator();\nenum.MoveNext();\nenum.Reset(); // throws\n\n// after\nvar blogs = context.Blogs.FromSqlRaw(\"SELECT * FROM Blogs\").ToList();","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call Reset() on EF Core query enumerators — always re-execute the query.","Materialize query results with ToList()/ToListAsync() before passing them to third-party code.","If a library calls Reset() internally, wrap results in a List<T> before passing them in.","Document in your codebase that EF enumerators are forward-only."],"tags":["enumerator","reset","fromsql","forward-only"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}