实体框架超时 - Entity Framework Timeouts

问题:

I am getting timeouts using the Entity Framework (EF) when using a function import that takes over 30 seconds to complete.使用需要超过 30 秒才能完成的函数导入时,我使用实体框架 (EF) 出现超时。 I tried the following and have not been able to resolve this issue:我尝试了以下方法,但无法解决此问题:

I added Default Command Timeout=300000 to the connection string in the App.Config file in the project that has the EDMX file as suggested here .我将Default Command Timeout=300000添加到具有 EDMX 文件的项目中的App.Config文件中的连接字符串,如建议here

This is what my connection string looks like:这是我的连接字符串的样子:

<add 
    name="MyEntityConnectionString" 
    connectionString="metadata=res://*/MyEntities.csdl|res://*/MyEntities.ssdl|
       res://*/MyEntities.msl;
       provider=System.Data.SqlClient;provider connection string=&quot;
       Data Source=trekdevbox;Initial Catalog=StarTrekDatabase;
       Persist Security Info=True;User ID=JamesTKirk;Password=IsFriendsWithSpock;
       MultipleActiveResultSets=True;Default Command Timeout=300000;&quot;"
    providerName="System.Data.EntityClient" />

I tried setting the CommandTimeout in my repository directly like so:我尝试直接在我的存储库中设置 CommandTimeout,如下所示:

private TrekEntities context = new TrekEntities();

public IEnumerable<TrekMatches> GetKirksFriends()
{
    this.context.CommandTimeout = 180;
    return this.context.GetKirksFriends();
}

What else can I do to get the EF from timing out?我还能做些什么来使 EF 超时? This only happens for very large datasets.这只发生在非常大的数据集上。 Everything works fine with small datasets.对于小数据集,一切正常。

Here is one of the errors I'm getting:这是我遇到的错误之一:

System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. System.Data.EntityCommandExecutionException:执行命令定义时出错。 See the inner exception for details.有关详细信息,请参阅内部异常。 ---> System.Data.SqlClient.SqlException: Timeout expired. ---> System.Data.SqlClient.SqlException:超时已过期。 The timeout period elapsed prior to completion of the operation or the server is not responding.操作完成前超时时间已过或服务器未响应。


OK - I got this working and it's silly what happened.好的 - 我开始工作了,发生的事情很愚蠢。 I had both the connection string with Default Command Timeout=300000 and the CommandTimeout set to 180. When I removed the Default Command Timeout from the connection string, it worked.我将Default Command Timeout=300000设置为Default Command Timeout=300000的连接字符串和设置为 180 的Default Command Timeout 。当我从连接字符串中删除Default Command Timeout ,它起作用了。 So the answer is to manually set the CommandTimeout in your repository on your context object like so:因此,答案是在您的上下文对象的存储库中手动设置 CommandTimeout,如下所示:

this.context.CommandTimeout = 180;

Apparently setting the timeout settings in the connection string has no effect on it.显然,在连接字符串中设置超时设置对其没有影响。


解决方案:

参考一: https://stackoom.com/question/Q9OL
参考二: Entity Framework Timeouts
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章