简体   繁体   中英

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 eg

<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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM