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

Dof*_*ofs 11 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
Run Code Online (Sandbox Code Playgroud)

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

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

Sim*_*ens 26

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

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

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

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

  • 为什么这是公认的答案? (6认同)