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

entity-framework

Question

I have had a breakdown on my webhost. Now finally it is up again, and I have yet to know what the technicians fixed. The problem is now I receive the 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

I have not changed anything, so could it be some permissions? I can still log onto my database with the same credentials so it is not the login info. Anyone have an idea?

UPDATE: I have found out that I get the error when I try to convert the IQuery to a list. I never received the error before, does this give any of you a clue what could be wrong?

1
11
6/12/2009 8:04:28 PM

Accepted Answer

The entity framework uses lazy evaluation. This means that the query is not actually executed against the database when you create it, it is executed when you actually need the data out. Consequently, the data context must still be open when you process the query.

Converting the query to an IList will force the query to be executed. If the data context is closed at this point you will get an error like this.

I can't explain why you didn't get this before if you haven't changed any code, but this is what I would be looking at.

Perhaps post your code, this might help diagnose the problem.

26
6/16/2009 2:09:22 PM

Popular Answer

I started getting this error after my computer shut down unexpectedly. After reading this thread, the answer by James Ellis-Jones led me to using SQL profiler to get the SQL being executed when .ToList() was called, and I ran the SQL in SQL Server Management Studio. This is the message that SQL Server returned:

SQL Server detected a logical consistency-based I/O error: incorrect checksum (expected: 0xb6a6f70e; actual: 0xb6a74f0e). It occurred during a read of page (1:50284) in database ID 5 at offset 0x000000188d8000 in file 'D:\Work\DATABASES\SQL2008R2\xxxxx.mdf'. Additional messages in the SQL Server error log or system event log may provide more detail. This is a severe error condition that threatens database integrity and must be corrected immediately. Complete a full database consistency check (DBCC CHECKDB). This error can be caused by many factors; for more information, see SQL Server Books Online.

So in my case, the unexpected shutdown left the database in an inconsistent state. I was able to successfully restore a database backup and the error went away.



Related Questions





Related

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