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)) { context.Database.SetCommandTimeout(300); 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.
I have been fighting with the idesigntimedbcontextfactory which does not support DI. This simple solution fixes my problems!
LikeLike
Thanks for your comment, I appreciate that!
LikeLike