实体框架关闭datareader时调用'Read'

时间:2009-06-09 19:24:36

标签: entity-framework

我的虚拟主机已经崩溃了。现在终于再次上升,我还不知道技术人员修复了什么。 问题是我现在收到错误:

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

我没有改变任何东西,所以它可能是一些权限吗?我仍然可以使用相同的凭据登录我的数据库,因此它不是登录信息。有人有想法吗?

更新:我发现当我尝试将IQuery转换为列表时出现错误。我之前从未收到过这个错误,这是否能给你们任何一个可能出错的线索?

3 个答案:

答案 0 :(得分:26)

实体框架使用延迟评估。这意味着在创建查询时实际上不会对数据库执行查询,而是在实际需要数据时执行查询。因此,在处理查询时,数据上下文仍必须处于打开状态。

将查询转换为IList将强制执行查询。如果此时数据上下文已关闭,您将收到类似的错误。

如果你没有更改任何代码,我无法解释为什么你之前没有得到这个,但这就是我要看的。

也许发布您的代码,这可能有助于诊断问题。

答案 1 :(得分:2)

我几乎遇到了这个错误,结果证明它是SQL数据库中的一致性错误。我通过在SQL Management Studio中对相关行中涉及的表运行查询来确认这一点,虽然我得到了数据,但也有一条错误消息告知了该问题。我想,与其他数据库运行的Entity Framework可能会发生类似的事情。

答案 2 :(得分:2)

我的计算机意外关闭后,我开始收到此错误消息。在阅读了这个帖子之后,James Ellis-Jones的回答让我使用SQL分析器来调用.ToList()时调用SQL,并在SQL Server Management Studio中运行SQL。这是SQL Server返回的消息:

SQL Server检测到基于逻辑一致性的I / O错误:校验和不正确(预期:0xb6a6f70e;实际:0xb6a74f0e)。它发生在数据库ID 5中的页面读取(1:50284)期间,文件'D:\ Work \ DATABASES \ SQL2008R2 \ xxxxx.mdf'中的偏移量为0x000000188d8000。 SQL Server错误日志或系统事件日志中的其他消息可能提供更多详细信息。这是严重错误情况,威胁数据库完整性,必须立即纠正。完成完整的数据库一致性检查(DBCC CHECKDB)。这个错误可能是由许多因素造成的;有关详细信息,请参阅SQL Server联机丛书。

所以在我的情况下,意外关机使数据库处于不一致状态。我能够成功恢复数据库备份,错误就消失了。

相关问题