运行Entity Framework Migration时遇到SQL超时问题

4

在 EF 迁移中运行 SQL() 命令时,我目前遇到了 SQL 超时的问题。

情况: 我要用一个表替换超过 50 个表,并需要将这些即将删除的表的数据转换到新表中。我按照以下方式组织了迁移:

1. 创建新表。

  1. 在同一次迁移中,使用 SQL() 函数运行迁移数据的 sql 脚本。

3. 删除所有旧表。

目前,迁移会产生以下错误:

System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

该错误发生在一个环境中,我为他们提供了一个安装程序,而他们在没有我的参与下运行它,因此我无法手动运行单个迁移,并在运行 SQL 脚本时暂停。

是否有办法更改连接的超时时间或解决此问题?

环境:

EF 6.0 Code First

SQL Server 2012

2个回答

8
看这个答案

Use Configuration.cs file to set custom time out:

internal sealed class Configuration :

DbMigrationsConfiguration<ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        ContextKey = "YourDbContext";

        // New timeout in seconds
        this.CommandTimeout = 60 * 5; 
    }
}

使用此方法,您只能更改迁移的超时时间,而不是每个人都使用默认连接字符串。


0
您应该能够在连接字符串中设置连接超时,就像这样:
Connection Timeout=180;

我们能不能只为迁移更改超时时间?在连接字符串中更改它也会影响应用程序用户,我不想这样做。 - AXMIM
发现在使用“包管理器控制台”时可以手动覆盖连接字符串,但不知道它如何用于自动迁移。 - AXMIM
2
-1 表示“连接超时”,而问题中的超时是“命令超时”,它不是连接字符串的一部分。 - thab

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接