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

十年热恋 提交于 2019-12-03 04:40:38

In the constructor of Configuration.cs class (in migration Folder) add the property CommandTimeout = Int32.MaxValue;

I restarted the SQL Server service (Win7 - Computer Management > Services and Applications > Services)

For me, the problem was that that migration script took a long time to run (15 minutes).

This is how I worked around the issue:
1. Run update-database -script (-force may be necessary)
2. Copy this SQL script output and run in SQL Server Management Studio

GrokSrc

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?

This is actually related to a bug in this version of 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. 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:

DbContextConfiguration.UseDatabaseNullSemantics = true

See here: 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.

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.

update-database -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.

An FYI EF Migrations pull their timeout from a separate configuration:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!