Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Update-Database" command fails with TimeOut exception

I'm using EF migrations and have a table with a lot of data. I need to change MaxLength of a concrete column (it hadn't length constraints).

ALTER TABLE MyDb ALTER COLUMN [MyColumn] [nvarchar](2) NULL

And this command fails with TimeOut exception. Tried to setup CommandTimeout i nDbContext constructor without any luck.

Is there any way to disable or setup timeout for Package Manager Console EF commands?

like image 844
deeptowncitizen Avatar asked Sep 01 '15 11:09

deeptowncitizen


4 Answers

Alternatively script out the change by using

Update-Database -script

You can then take the script and run it using SQL Management Studio against the database.

like image 82
Slavvy Avatar answered Oct 31 '22 04:10

Slavvy


Found solution by myself.

Since EF5 there is a new property CommandTimeout which is available from DbMigrationsConfiguration

internal sealed class MyMigrationConfiguration : DbMigrationsConfiguration<MyDbContext>
{
    public Configuration()
    {
        CommandTimeout = 10000; // migration timeout
    }
}
like image 27
deeptowncitizen Avatar answered Oct 31 '22 03:10

deeptowncitizen


I just had almost the exact same thing: timeout expired when trying to increase a column length. For me, using update-database had been working just fine an hour ago. The problem turned out to be an open transaction on the database and table I was trying to alter. Once I rolled back that transaction, the update-database command went through without problems.

like image 2
techturtle Avatar answered Oct 31 '22 05:10

techturtle


Ran into this same Update-Database command timeout issue with EntityFrameworkCore. Found that the -script argument no longer exists in EFCore. Documentation says you need to use Script-Migration.

EFCore Script-Migration

You specify the last migration that was applied to your database and it will generate a script for everything after it to get it current:

Script-Migration 20210307058985_addIndexesToClientTable

You can get the full migration id from your migration file names in your .net project, just take the .cs off the end. Alternatively, you can get it from your __EFMigrationsHistory table in your database.

If you need a script for creating a new DB it says to use 0:

Script-Migration 0 InitialCreate

For me it opened a temp .sql file in VS since I ran it from the package console manager and I was able to then copy and execute it in MSSQL Management Studio.

8/20/21 Update

for some reason my new migrations are requiring me to provide the optional -From and -To switches in order for it to generate a script:

Script-Migration -From 20210307058985_addIndexesToClientTable -To 20210820205751_modifyIndexesOnClientTable

like image 1
YetAnotherSteve Avatar answered Oct 31 '22 05:10

YetAnotherSteve