Add RetryOnFailure when connecting to DB in .NET Core

Sometimes connection to database server drops for few seconds especially for cloud, cluster or even servers configured in container. To avoid that simply enable retry on failure in startup.cs file in ConfigureServices method:

services.AddDbContext<ApplicationDbContext>(options =>
{
	options.UseSqlServer(authConnectionString, sql =>
		{
			sql.EnableRetryOnFailure(5, TimeSpan.FromSeconds(30), null);
		});
});

The code above will make 5 retries and wait 30 seconds between each retry

Leave a reply