Error selecting from large data table causing time out in EF + code first

守給你的承諾、 提交于 2019-12-11 15:09:24

问题


I am using EF code first model to get the data from data base table in which i have 400,000 records.

But when i use the LINQ query something like:

var urer = context.UserEntity.Where(c => c.FirstName.Contains('s'));

The above statement gives me all the user to whose first name contains 's'. But since this is a huge data base table, it is giving me the following error:

An existing connection was forcibly closed by the remote host

Please suggest me the best way to do it. I am assigning this data to gridview. I am thinking to get the first 500 each time. Is there any way to do it from EF side, so that i won't need to do it in sql.

Thanks


回答1:


1.add index on your column
2. increase timeout connection
You can create Store procedure USE LINQ call Store procedure

LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures)
http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

See this answer as well Calling a SQL Server stored procedure with linq service through c#




回答2:


  1. Get rid of EF
  2. set key in web.config common key for timeout replace 600

    try
    {
     conn.Open();
     mySqlCommand.Connection = conn;
      mySqlCommand.CommandTimeout=600;
    


来源:https://stackoverflow.com/questions/20479792/error-selecting-from-large-data-table-causing-time-out-in-ef-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!