Entity Framework Code First timeouts when using default SQL instance

I am seeing strange timeout behavior when connecting to a SQL Server 2012 Express default instance using Entity Framework - tested on 5.0.0 and 6.0.0.

If I use a server, by IP address, that is offline, EF seems to ignore any timeout values in the connection string.

This connection string always takes 73 seconds to timeout:

<connectionStrings>
<add name="TestContext" connectionString="Server=10.0.0.2;Database=TestDb;User ID=sa;MultipleActiveResultSets=True;Connection Timeout=5" providerName="System.Data.SqlClient" />

If I add a port, it takes 42 seconds:

<connectionStrings>
<add name="TestContext" connectionString="Server=10.0.0.2,1433;Database=TestDb;User ID=sa;MultipleActiveResultSets=True;Connection Timeout=5" providerName="System.Data.SqlClient" />

And any connectionstring with hostname takes 14 seconds, but if DNS resolves and host is offline; or online host doesn't have a SQL instance, it's back to 73 seconds e.g.

<connectionStrings>
<add name="TestContext" connectionString="Server=dev-pc;Database=TestDb;User ID=sa;MultipleActiveResultSets=True;Connection Timeout=5" providerName="System.Data.SqlClient" />

I have tried:

context.Database.CommandTimeout = 5;

with no change in results. This was expected since it doesn't even make it to the query.

Here is my inherited DbContext class:

namespace Test.DataLayer
{
    public class TestContext: DbContext
    {
        public DbSet<Person> People { get; set; }           

        static TestContext()
        {
            Database.SetInitializer<TestContext>(null);
        }    

        public TestContext()
            : base("Name=TestContext"){}           
    }
}

Questions:

Is this expected Entity Framework (or underlying provider) behavior?

Which timeout does it follow in this situation?

Since a 73 second timeout is too long, what can be done to speed up detection of offline SQL hosts?

Any other comments or suggestions will be appreciated.

Thanks,

Francois

Answers


CommandTimeout controls the time it waits for a specific command to execute (a SELECT for example); it will not have any impact when the server is unreachable and no command has been sent. Try ConnectionTimeout and see if it makes any difference.


Need Your Help

PHP - current folder / directory name

php printing path echo filenames

how can I get the current folder/directory name in PHP?

Finalizer launched while its object was still being used

c# .net garbage-collection finalization

Summary: C#/.NET is supposed to be garbage collected. C# has a destructor, used to clean resources. What happen when an object A is garbage collected the same line I try to clone one of its variable