【问题标题】:SQL Timeout when running an Entity Framework Migration运行实体框架迁移时 SQL 超时
【发布时间】:2016-11-18 21:00:12
【问题描述】:

我目前在 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 代码优先

SQL Server 2012

【问题讨论】:

    标签: entity-framework entity-framework-migrations


    【解决方案1】:

    看到这个answer。

    使用 Configuration.cs 文件设置自定义超时:

    内部密封类配置:

    DbMigrationsConfiguration<ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            ContextKey = "YourDbContext";
    
            // New timeout in seconds
            this.CommandTimeout = 60 * 5; 
        }
    }
    

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

    【讨论】:

      【解决方案2】:

      您应该能够在连接字符串中设置连接超时,例如:

      Connection Timeout=180;
      

      【讨论】:

      • 我们不能只为迁移更改超时时间吗?在连接字符串中更改它也会为应用程序的用户更改它,我不希望这样。
      • 发现使用“Package Manager Console”时可以手动覆盖连接字符串,但不知道如何用于自动迁移。
      • -1 因为这是“连接超时”,而问题中的超时是“命令超时”,它不是连接字符串的一部分
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      • 2015-05-11
      相关资源
      最近更新 更多