問題:
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="
Data Source=trekdevbox;Initial Catalog=StarTrekDatabase;
Persist Security Info=True;User ID=JamesTKirk;Password=IsFriendsWithSpock;
MultipleActiveResultSets=True;Default Command Timeout=300000;""
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