Linq-to-SQL 超时

发布于 2024-12-28 05:50:22 字数 206 浏览 6 评论 0原文

我在其中一个页面上收到一条错误消息,指出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

记忆で 2025-01-04 05:50:22

刚刚在智能感知中找到了答案:

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

这就是如何在每个查询的基础上增加查询的超时时间,就像修改连接字符串或数据上下文一样。

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.

浅暮の光 2025-01-04 05:50:22

@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.

女中豪杰 2025-01-04 05:50:22

以下是 .Net 4.5 的最新语法。

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

Here is the latest syntax for .Net 4.5.

    MyDbContext context = new MyDbContext();
    context.Database.SetCommandTimeout(300);
烟凡古楼 2025-01-04 05:50:22

汤姆·古伦的回答很好。

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

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.

鹊巢 2025-01-04 05:50:22

在增加 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文