Entity Framework : handle error the underlying provider fail to open

1.1k Views Asked by At

What is the best way to handle a network error in Entity Framework? Each time I have a poor internet connection to the Azure SQL database, I get the following error:

The underlying provider failed on Open.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

I know this error is caused by the stability of the internet connection at work, so I'm not trying to fix the error but to handle it gracefully to the user.

I could use a try catch but I will have to modify every line in every class that tries to use the connection.

One alternative is to use a repository for the connection and handle the error there, but it's going to take a lot of time to refactor every part of the code. So I'm looking for alternatives.

Example of

Context DB = new Context();

List<Persona> lista = new List<Persona>();
List<Persona> listaTemporal = new List<Persona>();

listaTemporal = DB.Personas.Where(one => one.Activo == true && 
                                         one.FechaEstadoInicialSAP != null)
                           .ToList();  // it stops here because of the exception
1

There are 1 best solutions below

1
On BEST ANSWER

Here is a good pointer with code samples for implementing retry strategy: https://msdn.microsoft.com/en-us/data/dn456835.aspx?f=255&MSPPError=-2147217396

Thanks, Mihaela