I have an ASP.NET Core MVC application where I inject a DbContext into a controller. I want to know if there is a way to intercept the connection timeout exception while constructing the DbContext returning a specific error response code.
I noticed that the exception is thrown inside the method AddDbContext in the Startup class:
services.AddDbContext<MyDbContext>(options =>
{
options.UseMySql(Configuration.GetConnectionString("DefaultConnection"),
ServerVersion.AutoDetect(Configuration.GetConnectionString("DefaultConnection")), o =>
{
o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
});
});
EDIT: Could the exception be thrown from the AutoDetect option which opens a connection to the DB to retrieve its version? I'm using the Pomelo.EntityFrameworkCore.MySql NuGet package, version 5.0.0
Thanks
SaveChangesis called. If you want to catch a timeout exception use a normal exception handling block around theToListorSaveChangescals