首页 文章

ToList()的linq超时,由于使用DbContext而无法使用AsQueryable

提问于
浏览
0

在松散类型的LINQ查询中调用 ToList() 时,我一直在获得linq超时的零星错误 . 我想通过从方法返回 IQueryable 并稍后枚举来优化它 . 但是,作为团队策略,我们使用带有 DbContext 的using语句;因此,一旦我退出using子句,我就无法枚举调用方法中的 IQueryable .

我的问题是:是否建议使用 DbContext 避开 using 语句并让EF处理连接关闭和垃圾回收?
如果是,那么最好初始化 context ?何时何地处置它?

如果没有,如何修复 ToList() 上的超时错误?

非常感谢 .

Error I get:

System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时发生错误 . 有关详细信息,请参阅内部异常---> System.Data.SqlClient.SqlException:超时已过期 . 操作完成之前经过的超时时间或服务器没有响应 . 在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection)处于System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)的System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()处于System.Data .SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand的cmdHandler,SqlDataReader的数据流,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)在System.Data.SqlClient.SqlDataReader.ConsumeMetaData()在System.Data.SqlClient.SqlDataReader.get_MetaData()在System.Data System.Data.SqlClient.SqlCommand.RunExecuteReader(在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async)中的.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString) CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,DbAsyncResult result)at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader的System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String method)中的System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法) CommandBehavior.Infrastructure.Interception.InternalDispatcher1中的System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b__c(DbCommand t,DbCommandInterceptionContext1 c)处的System.Data.Common.DbCommand.ExecuteReader(CommandBehavior行为)处的CommandBehavior行为)系统中的System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand命令,DbCommandInterceptionContext interceptionContext)中的.Dispatch [TTarget,TInterceptionContext,TResult](TTarget目标,Func3操作,TInterceptionContext interceptionContext,Action3执行,Action`3已执行) System.Data.Co上的.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior) System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)中的mmon.DbCommand.ExecuteReader(CommandBehavior行为)

Code:

using (MyEntities context = new MyEntities())
{
myQuery =       from tableOne in context.TableOne
join tableTwo in context.TableTwo
on tableOne.My_OID equals tableTwo.My_OID
join tableThree in context.TableThree
on tableTwo.Location_OID equals     tableThree.Location_OID
join tableFour in context.TableFour
on tableTwo.JobPosting_OID equals tableFour.JobPosting_OID
join tableFive in context.TableFive
on tableFour.Client_OID equals tableFive.Client_OID
where 
tableOne.Editable_f == true && tableOne.Active_f == true
&& tableTwo.Active_f == true
&& tableFive.Active_f == true
&& tableThree.Active_f == true
&& tableFour.Active_f == true
&& tableFive.Employee_OID == givenUserID
&& tableThree.Employee_OID == givenUserID
&& tableFive.SchemaName.Equals("MySchema")
&& tableThree.SchemaName.Equals("MySchema")
orderby tableOne.MyName ascending
select new MyDTO
{
PropOne = tableOne.My_OID,
PropTwo = tableOne.MyName,
PropThree = tableOne.Create_By
};

return myQuery.ToList<MyDTO>();

最后,我可以通过某种方式优化查询本身吗?我只返回TableOne中的值,所以我可以摆脱与其他表的连接并使用 Any<> 或一些LINQ等效的SQL EXISTS

任何帮助是极大的赞赏 . 非常感谢!

1 回答

  • 1

    http://blog.jongallant.com/2012/10/do-i-have-to-call-dispose-on-dbcontext.html

    这是关于处理EF dbcontext的好文章 . 简而言之,EF为您管理连接,您无需手动执行此操作 . 但是,我建议在关闭DbContext之后尝试迭代IQueryable之前优化查询 . 使用Visual Studio Intellitrace或其他分析器捕获Sql Server语句,并查看查询有什么问题 . 实体框架中也有一些优化技巧,例如:急切加载 - Include()方法或AsNoTracking()方法 .

相关问题