Increase timeout Migrations Entity Framework Core

Use a different connection timeout when running your migrations with EF Core.


Sometimes a migration takes more time then your default timeout of your database connection. In such a case you do want to increase the timeout for the migration to be able to complete, but you do not want to change the timeout for your normal operations.

You can increase the timeout before you call the Migrations with by setting the timeout on the Database context:

using (var context = new DispatchingDbContext(_configuration))
    await context.Database.MigrateAsync().ConfigureAwait(false);

When running your migrations you see the following error:

+ $exception {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

This way do only have a different timeout when you are running the migrations on the database.


2 thoughts on “Increase timeout Migrations Entity Framework Core”

  1. I have been fighting with the idesigntimedbcontextfactory which does not support DI. This simple solution fixes my problems!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: