DEV Community

Discussion on: Top 10 Mistakes Developers Make in EF Core

Collapse
 
stevsharp profile image
Spyros Ponaris

Great post! I’d like to add to the discussion on handling transient database errors in EF Core. One powerful strategy is using the EnableRetryOnFailure method when configuring the DbContext

services.AddDbContext(options =>
{
options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"], sqlOptions =>
{
sqlOptions.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorNumbersToAdd: null // Optionally add custom error numbers
);
});
});

This approach improves application reliability and resilience by reducing downtime caused by short-lived issues.