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?
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.
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
}
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With