简体   繁体   English

EF 代码优先 - 超时已过。 完成前超时时间已过

[英]EF Code First - Timeout expired. The timeout period elapsed prior to completion

Apology for this strangely worded question.为这个措辞奇怪的问题道歉。 I don't know what the actual problem is but hopefully someone can give me some insights.我不知道实际问题是什么,但希望有人能给我一些见解。

I am getting the following error when trying to run migrations:尝试运行迁移时出现以下错误:

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

It is interesting to note that on my laptop this is not happening but on my VM (azure - large) this is happening with 100% failure rate.有趣的是,在我的笔记本电脑上没有发生这种情况,但在我的 VM(天蓝色 - 大型)上发生了 100% 的故障率。

I am using Ef 6.0.0 -rc1 .我正在使用Ef 6.0.0 -rc1 Please note, updating EF is not an option.请注意,更新 EF 不是一个选项。 If updating to EF 6.0.0 or 6.0.1 I will get the following error with 100% failure rate:如果更新到 EF 6.0.0 或 6.0.1,我将以 100% 的失败率收到以下错误:

Errors during Code First add-migration Code First 添加迁移期间的错误

I have also timed the error.我也对错误进行了计时。 It takes about 1.5 min to trigger the error.触发错误大约需要 1.5 分钟。 When running with -Verbose flag it was trying to create 200 tables with indexes.当使用-Verbose标志运行时,它试图创建 200 个带索引的表。 Copying the sql query and excuting it in SSMS takes 5 secs.复制 sql 查询并在 SSMS 中执行它需要 5 秒。

A few things that I have tired but didn't work:一些我累了但没用的事情:

1) Setting ObjectContext.CommandTimeout = 36000 // 10 hours! 1) 设置ObjectContext.CommandTimeout = 36000 // 10 hours! as indicated here:如此处所示:

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

2) Setting timeout in connection string in "web.config": 2)在“web.config”中的连接字符串中设置超时:

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

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

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

4) Setting "remote query timeout" on sql server 4)在sql服务器上设置“远程查询超时”

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

So what is happening?那么发生了什么? How come CommandTimeout is not being respected?为什么CommandTimeout没有受到尊重? Any suggestions?有什么建议么?

Full trace as follows:全迹如下:

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.

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

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

For me, the problem was that that migration script took a long time to run (15 minutes).对我来说,问题是迁移脚本需要很长时间才能运行(15 分钟)。

This is how I worked around the issue:我是这样解决这个问题的:
1. Run update-database -script ( -force may be necessary) 1. 运行update-database -script (可能需要-force
2. Copy this SQL script output and run in SQL Server Management Studio 2. 复制此 SQL 脚本输出并在 SQL Server Management Studio 中运行

An FYI EF Migrations pull their timeout from a separate configuration:仅供参考的 EF 迁移从单独的配置中提取超时:

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

Change the 900 to a something higher, all of the other SQL timeout changes (web.config, etc.) did not do anything, this worked for me.将 900 更改为更高的值,所有其他 SQL 超时更改(web.config 等)都没有做任何事情,这对我有用。

I ran into this in my production environment because it was producing queries like the ones here: Why does Entity Framework 6 generate complex SQL queries for simple lookups?我在我的生产环境中遇到了这个问题,因为它正在生成类似于这里的查询: 为什么 Entity Framework 6 为简单的查找生成复杂的 SQL 查询?

This is actually related to a bug in this version of EF: https://entityframework.codeplex.com/workitem/2083这实际上与此版本 EF 中的一个错误有关: https : //entityframework.codeplex.com/workitem/2083

They changed the default null semantics from 5 to 6 so I'm guessing you had the same problem I did after upgrading.他们将默认的空语义从 5 更改为 6,因此我猜您在升级后遇到了同样的问题。 My machine had a fraction of the data as my remote installation and until I got to production I didn't know I had a performance issue.我的机器有一小部分数据作为我的远程安装,在我投入生产之前,我不知道我遇到了性能问题。 The queries will often produce a table scan which will time out for larger tables.查询通常会产生表扫描,这对于较大的表会超时。

To change it back so that it works like EF5 you have to set:要将其改回以使其像 EF5 一样工作,您必须设置:

DbContextConfiguration.UseDatabaseNullSemantics = true

See here: http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbcontextconfiguration.usedatabasenullsemantics(v=vs.113).aspx请参阅此处: http : //msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbcontextconfiguration.usedatabasenullsemantics(v=vs.113).aspx

The bug was fixed in EF 6.1 but you still have to set the option above to get simple where conditions.该错误已在 EF 6.1 中修复,但您仍然必须设置上面的选项以获得简单的 where 条件。

I just had the same exact issue, i know this thread is a year old but maybe it will help someone else.我刚刚遇到了同样的问题,我知道这个线程已经有一年了,但也许它会帮助其他人。

I was trying to create the database in entity 5 using the Package Manager Console using the connection string below.我试图使用包管理器控制台使用下面的连接字符串在实体 5 中创建数据库。

update-database -ConfigurationTypeName My.Project.EF.Migrations.Configuration -ConnectionStringName MyDatabaseDev -ProjectName Project.Name -StartUpProjectName Database.Name更新数据库 -ConfigurationTypeName My.Project.EF.Migrations.Configuration -ConnectionStringName MyDatabaseDev -ProjectName Project.Name -StartUpProjectName Database.Name

Each time i ran it i got the error below.每次我运行它时,我都会收到以下错误。

Timeout expired.超时已过。 The timeout period elapsed prior to completion of the operation or the server is not responding.操作完成前超时时间已过或服务器未响应。

To fix it i simply added the -force parameter and it went right through.为了修复它,我简单地添加了-force参数,它就通过了。

I ran into this error as well and tried using various command timeout settings and found nothing was working.我也遇到了这个错误,并尝试使用各种命令超时设置,但发现没有任何效果。 After awhile I discovered that my issue wasn't a timeout issue but a SQL transaction issue.过了一会儿,我发现我的问题不是超时问题,而是 SQL 事务问题。 Apparently, my code was conflicting with a SQL transaction that I had previously ran.显然,我的代码与我之前运行的 SQL 事务发生冲突。 At the time, the transaction was not committed or rolled back.当时,事务未提交或回滚。 Once I ran my COMMIT statement the issue resolved itself.一旦我运行我的 COMMIT 语句,问题就会自行解决。

Remember to always COMMIT or ROLLBACK your transactions.请记住始终提交或回滚您的交易。

Verify if no has some transaction openned, this happened today with me and caused timeout to execute Scaffold-DbContext command, Because the DataBase is locked and the Scaffold-DbContext command can´t run.验证是否没有打开某些事务,这发生在我身上并导致执行 Scaffold-DbContext 命令超时,因为数据库被锁定并且 Scaffold-DbContext 命令无法运行。

Close all transaction openned and try again.关闭所有打开的事务并重试。

暂无
暂无

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

相关问题 超时已过。 在操作完成之前超时时间已过 - Timeout expired. The timeout period elapsed prior to completion of the operation ExecuteQueryin linq:超时已过期。 操作完成之前经过的超时时间或服务器没有响应 - ExecuteQueryin linq :Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding SQL Server异常:超时已过期。 操作完成之前经过的超时时间或服务器未响应 - SQL Server Exception:Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding Linq Count() 超时 - 执行超时已过期。 操作完成前超时时间已过或服务器未响应 - Linq Count() timing out -Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding 超时已过。 在操作完成之前超时时间已过或服务器没有响应。 该语句已终止 - Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated 超时时间已到。 操作完成之前经过的超时时间或服务器未响应 - Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding 超时时间已到。 操作完成之前经过的超时时间或服务器未响应 - Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding 在sis中执行软件包会导致超时过期。 操作完成之前经过的超时时间或服务器未响应 - Execute package in ssis causes Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding 超时时间已到。 在操作完成之前超时或服务器没有响应。 在远程Windows .net上工作 - Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. working on remote windows .net 超时已过。 操作完成前超时时间已过或服务器未响应 - Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM