繁体   English   中英

C# entityframework 核心抛出 Timeout expired。 操作完成前超时时间已过或服务器无响应

[英]C# entityframework core throwing Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding

我正在使用以下使用 EF 核心的 linq 查询。 并收到以下错误:

Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

不知道我在这里做错了什么。

ECUserId 是 varchar 列,并且此表上没有为此列设置索引。 还检查了排序规则并将其设置为区分大小写的“SQL_Latin1_General_CP1_CI_AS”。

string ECUserId = "TN504060"
var userStandby = await _context.UserStandby
                                .Where(standBy => ECUserId.Equals(standBy.ECUserId, 
                                       StringComparison.OrdinalIgnoreCase))
                                .FirstOrDefaultAsync();

userStandby 表有大约 126580 条记录。

谁能帮我解决上述超时错误?

在此列上创建了一个索引,但仍然出现相同的超时错误:

GO
CREATE NONCLUSTERED INDEX [IX_UserStandby_ECUserId]
    ON [dbo].[UserStandby]([ECUserId] ASC);

linq 查询未生成带有 where 条件的 sql 查询。 请问我在这里做错了什么?

谢谢

您可以在设置 DbContext 时设置更长的超时时间:

        services.AddDbContext<MyDbContext>(config =>
          config.UseSqlServer(
             "MyConnectionString",
             providerOptions =>
             {
                 providerOptions.CommandTimeout(180); <-- Timeout in seconds
             })
        );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM