Linq-to-SQL 超时
我在其中一个页面上收到一条错误消息,指出 linq 查询由于花费的时间太长而超时。它使页面无法使用。
这是一个报告页面,管理员每天只能访问一次。削减这个查询是不可避免的,它只需要对大量数据进行排序。
我读过的解决此问题的解决方案是增加数据上下文中的超时属性,但我想避免这样做,因为这会改变整个网站的超时属性。
有没有办法为各个页面设置更长的超时时间?
I've been receiving an error on one of my pages that the linq query has timed out as it is taking too long. It makes the page unusable.
It's a reports page which is only accessed by administrators around once a day. It's unavoidable to trim this query down at all, it just has to sort through a lot of data.
Solutions to fix this I've read are by increasing the timeout property in the data context, but I'd like to avoid doing that as it would change it for the entire website.
Is there any way to set a larger time out for individual pages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
刚刚在智能感知中找到了答案:
这就是如何在每个查询的基础上增加查询的超时时间,就像修改连接字符串或数据上下文一样。
Just found the answer playing with intellisense:
This is how you can increase the time out for a query on a per query basis as supposed to modifying the connection strings or data contexts.
@Tom Gullens 的答案在
EF6
上对我不起作用,我必须深入到
DbContext
上的数据库Ecom.Database.CommandTimeout = 120;
希望这能为您节省一些时间。
@Tom Gullens answer didn't work for me on
EF6
I had to drill down to Database on
DbContext
Ecom.Database.CommandTimeout = 120;
Hope this saves you some time.
以下是 .Net 4.5 的最新语法。
Here is the latest syntax for .Net 4.5.
汤姆·古伦的回答很好。
另一位受访者提到在连接字符串中设置连接超时。
我想提醒一下,连接字符串的连接超时属性不是命令超时。当你仔细思考时,这一点就更明显了。这是一个常见的错误。
Tom Gullen's answer is a good one.
Another respondent mentioned setting the Connect Timeout in the connection string.
I wanted to caution that the connection timeout property of the connection string is NOT the command timeout. It's more obvious when you think about it. It's a common mistake.
在增加 SQL 超时之前,始终值得评估您的索引策略。根据我的经验,针对索引良好的表和列的查询很少会超时。
德斯·欧文
Before increasing a SQL timeout it is always worth evaluating your indexing strategy. It is rare, in my experience, that a query against a well indexed table and columns would timeout.
Des Owen