【问题标题】:EF Code First - Timeout expired. The timeout period elapsed prior to completionEF 代码优先 - 超时已过期。完成前超时时间已过
【发布时间】:2013-11-23 21:26:00
【问题描述】:

为这个措辞奇怪的问题道歉。我不知道实际问题是什么,但希望有人能给我一些见解。

我在尝试运行迁移时收到以下错误:

Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

有趣的是,在我的笔记本电脑上没有发生这种情况,但在我的虚拟机(天蓝色 - 大型)上发生这种情况的失败率是 100%。

我正在使用 Ef 6.0.0 -rc1。请注意,更新 EF 不是一种选择。如果更新到 EF 6.0.0 或 6.0.1,我将收到以下错误,失败率为 100%:

Errors during Code First add-migration

我也为错误计时。触发错误大约需要 1.5 分钟。当使用-Verbose 标志运行时,它试图创建 200 个带有索引的表。复制 sql 查询并在 SSMS 中执行需要 5 秒。

我累了但没用的几件事:

1) 设置ObjectContext.CommandTimeout = 36000 // 10 hours!,如下所示:

https://stackoverflow.com/a/6234593/305469

2) 在“web.config”中设置连接字符串超时:

connectionString="Data Source=localhost;Initial Catalog=myDB;Integrated Security=SSPI;Connection Timeout=36000"

3) 设置“machine.config”事务maxTimeout:

<system.transactions> <machineSettings maxTimeout="00:00:00" /> </system.transactions>

4) 在sql server上设置“远程查询超时”

USE MyDB;
GO
EXEC sp_configure 'remote query timeout', 0 ;
GO
RECONFIGURE ;
GO

那么发生了什么?为什么CommandTimeout 不被尊重?有什么建议吗?

完整的跟踪如下:

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

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
   at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass32.<ExecuteStatements>b__2e()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
   at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
ClientConnectionId:8cbbc70c-8182-417e-9aca-4603f797340d
Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

【问题讨论】:

  • 可以添加stacktrace吗?
  • 当然@DenPakizh,我已将堆栈跟踪添加到问题中。
  • 1) 从堆栈跟踪看起来迁移不使用 ObjectContext 2) AFAIK 这个超时是建立连接的超时而不是与执行实际语句相关的超时 3,4) 我的猜测是 EF代码迁移在 SQLCommand 上设置了 90 秒的超时时间,这就是控制超时时间量的原因 您可以运行 SQL Server 分析器来查看执行的最后一个查询是什么吗?或者使用technet.microsoft.com/en-us/library/ms175518.aspx定位阻塞?

标签: c# sql-server entity-framework entity-framework-migrations


【解决方案1】:

在 Configuration.cs 类的构造函数中(在迁移文件夹中)添加属性 CommandTimeout = Int32.MaxValue;

【讨论】:

【解决方案2】:

我重新启动了 SQL Server 服务(Win7 - 计算机管理 > 服务和应用程序 > 服务)

【讨论】:

  • 在简单查询开始无缘无故引发此错误时帮助我,ty。
  • @FLCL 如果这有助于查询没有无缘无故地提出它,原因是执行时间太长。否则,提高这将无济于事,默认时间对于“简单”查询来说绰绰有余。也许那个查询不是那么简单,启动分析器并查看执行时间。
  • @DanteTheSmith 可能是由于事务未正确关闭而导致锁定。
【解决方案3】:

对我来说,问题在于迁移脚本需要很长时间才能运行(15 分钟)。

这就是我解决此问题的方法:
1. 运行update-database -script(可能需要-force
2. 复制此 SQL 脚本输出并在 SQL Server Management Studio 中运行

【讨论】:

    【解决方案4】:

    FYI EF 迁移从单独的配置中提取超时:

    public class MyContextConfiguration : DbMigrationsConfiguration<MyContext>
    {
        public MyContextConfiguration()
        {
            CommandTimeout = 900;
        }
    }

    将 900 更改为更高的值,所有其他 SQL 超时更改(web.config 等)都没有做任何事情,这对我有用。

    【讨论】:

    • 危险,因为它会让每个查询或命令在最终引发异常之前花费更多时间。
    • 是的,我只为部署更改它,然后恢复正常。但是对于大型数据库甚至是很小的更改,这都有效。作为一名尝试在框架内工作的开发人员,这个过程产生了奇迹。
    【解决方案5】:

    我在我的生产环境中遇到了这个问题,因为它会产生类似于这里的查询:Why does Entity Framework 6 generate complex SQL queries for simple lookups?

    这其实和EF这个版本的一个bug有关:https://entityframework.codeplex.com/workitem/2083

    他们将默认的空语义从 5 更改为 6,所以我猜你在升级后遇到了同样的问题。我的机器只有一小部分数据作为我的远程安装,在我投入生产之前我不知道我遇到了性能问题。查询通常会产生表扫描,对于较大的表会超时。

    要将其改回以使其像 EF5 一样工作,您必须设置:

    DbContextConfiguration.UseDatabaseNullSemantics = true

    请看这里:http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbcontextconfiguration.usedatabasenullsemantics(v=vs.113).aspx

    该错误已在 EF 6.1 中修复,但您仍需要设置上面的选项以简化 where 条件。

    【讨论】:

      【解决方案6】:

      我刚刚遇到了同样的问题,我知道这个帖子已经有一年了,但也许它会对其他人有所帮助。

      我试图使用包管理器控制台使用下面的连接字符串在实体 5 中创建数据库。

      update-database -ConfigurationTypeName My.Project.EF.Migrations.Configuration -ConnectionStringName MyDatabaseDev -ProjectName Project.Name -StartUpProjectName Database.Name

      每次运行时都会出现以下错误。

      超时。在操作完成之前超时时间已过或服务器没有响应。

      为了解决这个问题,我简单地添加了 -force 参数,它就通过了。

      【讨论】:

      • 添加 -force 对我不起作用。我必须应用的迁移很大,并且我使用的是 EF6,与上面提到的 6.1 相比效率低下。在 VS 项目 Migrations 文件夹中有类 Configuration.cs。在该类的构造函数中,我添加了属性 CommandTimeout = Int32.MaxValue; 该更改允许 update-database 命令最终完成而没有错误。
      • @voidmain -force 对我也不起作用,您的 CommandTimeout 设置效果很好。谢谢,值得评论。
      【解决方案7】:

      我也遇到了这个错误,并尝试使用各种命令超时设置,但发现没有任何效果。过了一会儿,我发现我的问题不是超时问题,而是 SQL 事务问题。显然,我的代码与我之前运行的 SQL 事务冲突。当时,事务没有提交或回滚。一旦我运行我的 COMMIT 语句,问题就会自行解决。

      请记住始终提交或回滚您的交易。

      【讨论】:

        猜你喜欢
        • 2015-12-01
        • 2011-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        • 2012-07-24
        相关资源
        最近更新 更多