实体框架超时 [英] Entity Framework Timeouts
问题描述
使用实体框架(EF)在使用完成超过30秒的函数导入时,我会超时。我尝试以下操作,无法解决此问题:
I am getting timeouts using the Entity Framework (EF) when using a function import that takes over 30 seconds to complete. I tried the following and have not been able to resolve this issue:
我添加了默认命令超时= 300000
具有EDMX文件的项目中的 App.Config 文件中的连接字符串,这是我的连接字符串看起来像这样:
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.
<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" />
我尝试在我的存储库中直接设置CommandTimeout,如下所示:
I tried setting the CommandTimeout in my repository directly like so:
private TrekEntities context = new TrekEntities();
public IEnumerable<TrekMatches> GetKirksFriends()
{
this.context.CommandTimeout = 180;
return this.context.GetKirksFriends();
}
还有什么可以让EF从超时开始?这只适用于非常大的数据集。一切都适用于小数据集。
What else can I do to get the EF from timing out? 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:执行命令定义时发生错误。查看内部例外情况。 ---> System.Data.SqlClient.SqlException:超时过期。在完成操作或服务器之前经过的超时时间没有响应。
System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
默认命令超时= 300000 和CommandTimeout设置为180.当我删除默认命令超时
从连接字符串,它工作。所以答案是在你的上下文对象上手动设置CommandTimeout,如下所示:
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. So the answer is to manually set the CommandTimeout in your repository on your context object like so:
this.context.CommandTimeout = 180;
显然设置连接字符串中的超时设置对此没有影响。
Apparently setting the timeout settings in the connection string has no effect on it.
推荐答案
有一个已知的错误,指定EF连接字符串中的默认命令超时。
There is a known bug with specifying default command timeout within the EF connection string.
http://bugs.mysql.com/bug.php?id=56806
从连接字符串中删除值,并将其设置在数据上下文对象本身上。如果您从连接字符串中删除冲突的值,则此操作将起作用。
Remove the value from the connection string and set it on the data context object itself. This will work if you remove the conflicting value from the connection string.
this.context.Database.SetCommandTimeout(180);
实体框架6:
Entity Framework 6:
this.context.Database.CommandTimeout = 180;
实体框架5:
Entity Framework 5:
((IObjectContextAdapter)this.context).ObjectContext.CommandTimeout = 180;
实体框架4及以下:
Entity Framework 4 and below:
this.context.CommandTimeout = 180;
这篇关于实体框架超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!