C# Linq-to-SQL 超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8969751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:23:24  来源:igfitidea点击:

Linq-to-SQL Timeout

c#asp.netlinq-to-sqltimeout

提问by Tom Gullen

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.

我在我的一个页面上收到一个错误,提示 linq 查询超时,因为它花费的时间太长。它使页面无法使用。

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?

有没有办法为单个页面设置更大的超时时间?

采纳答案by Tom Gullen

Just found the answer playing with intellisense:

刚刚找到了使用智能感知的答案:

using (MainContext db = new MainContext())
{
    db.CommandTimeout = 3 * 60; // 3 Mins
}

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.

这是您可以在每个查询的基础上增加查询超时的方法,以修改连接字符串或数据上下文。

回答by Larry Smith

Tom Gullen's answer is a good one.

Tom Gullen 的回答很好。

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.

另一位受访者提到在连接字符串中设置连接超时。
我想提醒的是,连接字符串的连接超时属性不是命令超时。仔细想想就更明显了。这是一个常见的错误。

回答by Des Owen

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.

在增加 SQL 超时之前,评估您的索引策略总是值得的。根据我的经验,对索引良好的表和列的查询很少会超时。

Des Owen

欧文

回答by Matas Vaitkevicius

@Tom Gullens answer didn't work for me on EF6

@Tom Gullens 的回答对我不起作用 EF6

I had to drill down to Database on DbContext

我不得不深入到数据库 DbContext

Ecom.Database.CommandTimeout = 120;

Ecom.Database.CommandTimeout = 120;

Hope this saves you some time.

希望这可以为您节省一些时间。

回答by ZaneDarken

Here is the latest syntax for .Net 4.5.

这是 .Net 4.5 的最新语法。

    MyDbContext context = new MyDbContext();
    context.Database.SetCommandTimeout(300);