When the datareader is closed, the entity framework calls 'Read'.



My webhost has broken down for me. Finally, it is working again, albeit I'm not sure what the specialists did to repair it. The issue is that I now get the following error:

Calling 'Read' when the data reader is closed is not a valid operation. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Calling 'Read' when the data reader is closed is not a valid operation.

Source Error: 

 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

    Stack Trace:   

[InvalidOperationException: Calling 'Read' when the data reader is closed is not a valid operation.]
   System.Data.Common.Internal.Materialization.Shaper`1.StoreRead() +93
   System.Data.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +30
   System.Linq.Enumerable.Single(IEnumerable`1 source) +119
   System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2(IEnumerable`1 sequence) +5
   System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +25
   System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +43
   System.Linq.Queryable.Count(IQueryable`1 source) +240
   BusinessLayer.Car.GetCarCount() in xxx
   UserControls_SiteInfo.Page_Load(Object sender, EventArgs e) +225
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

If nothing has changed on my end, might it be a permission issue? It is not the login information since I can still access my database with the same credentials. Anyone have a solution?

UPDATE: I've discovered that trying to turn the IQuery into a list causes the problem. Does this provide any of you a hint as to what could be wrong as I've never had the problem before?

6/12/2009 8:04:28 PM

Accepted Answer

In the entity framework, evaluation is done lazily. In other words, the query is not really run against the database when it is created; rather, it is run when the data is truly needed. Consequently, when you execute the query, the data context must still be open.

The query will have to be run if it is changed to an IList. You will see an error similar to this one if the data context is closed at this point.

If you haven't altered any code, I'm not sure why you didn't receive this previously, but this is what I'd be looking at.

Posting your code might perhaps aid in issue diagnosis.

6/16/2009 2:09:22 PM

Popular Answer

This issue first appeared when my computer abruptly shut off. After reading this discussion, James Ellis-Jones' response inspired me to use SQL profiler to determine when SQL was being performed. I executed the SQL in SQL Server Management Studio after using ToList(). The response from SQL Server is as follows:

A logical consistency-based I/O fault was discovered by SQL Server: an erroneous checksum (expected: 0xb6a6f70e; actual: 0xb6a74f0e). At location 0x000000188d8000 in file 'D:WorkDATABASESSQL2008R2xxxxx.mdf', it happened during a read of page (1:50284) in database ID 5. More information can be available in additional messages in the system event log or SQL Server error log. This serious error situation must be fixed right away since it jeopardizes the integrity of the database. do a comprehensive database consistency check (DBCC CHECKDB). There are several potential causes for this issue; for additional details, see SQL Server Books Online.

In my situation, the abrupt shutdown resulted in an inconsistent database. The problem vanished when I was successful in restoring a database backup.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow