EF6 connection lasts a long time

So I have the code for connecting to the database, which is pretty standard.

In the DbContext I create, I am trying to query a table.

When I create the DbContext, instead of the actual database url, I put in some url that doesn't exist.

So I expect to fail at the moment, the problem is I can't get it to run faster than 30 seconds.

I am getting a bunch of first change exceptions from System.Data.SqlClient.SqlInternalConnection

  

A network related or instance specific error occurred while establishing a connection to SQL Server. The server was not found or was not available. Verify that the instance name is correct and configure SQL Server for remote connection. (provider: Named Pipes provider, error: 40 - Could not open connection to SQL Server)

         

at System.Data.SqlClient.SqlInternalConnection.OnError (SqlException, Boolean breakConnection, Action`1 wrapCloseInAction)

  

I tried to create custom DbConfiguration

and DbExecutionPolicy but it doesn't seem to get called until it tries to connect a whole bunch of times.

public class InternalsDbConfiguration : DbConfiguration
{
    public InternalsDbConfiguration()
    {
        this.SetExecutionStrategy("System.Data.SqlClient", () =>
        {
            var r = new MyExecutionStrategy();
            return r;
        });
    }
}

public class MyExecutionStrategy : DbExecutionStrategy
{
    protected override bool ShouldRetryOn(Exception exception)
    {
        var s = exception as SqlException;
        if (s == null)
            return false;
        if (s.Number == 53)
            return false;
        return true;
    }
}

      

I just can't get it to fail to connect faster than 30 seconds because it keeps spinning through some process that I can't influence, looks like a retry policy without any backtracking, 15 times before it gives up and calls mine MyConnectionStratagy

to figure out what to do.

Any ideas? I wish he would fail the first time and not try.

+3


source to share


1 answer


Try adding Connection Timeout=5;

to your connection string, if you want to increase for all subsequent commands useDbContext.Database.CommandTimeout = 60;



+3


source







All Articles