简体   繁体   English

使用默认SQL实例时的实体框架代码优先超时

[英]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. 使用实体框架连接到SQL Server 2012 Express默认实例时,我看到奇怪的超时行为-在5.0.0和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. 如果按IP地址使用处于脱机状态的服务器,则EF似乎会忽略连接字符串中的任何超时值。

This connection string always takes 73 seconds to timeout: 此连接字符串始终需要73秒才能超时:

<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: 如果添加端口,则需要42秒:

<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; 并且任何具有主机名的连接字符串都需要14秒钟,但是如果DNS解析并且主机处于脱机状态; or online host doesn't have a SQL instance, it's back to 73 seconds eg 或在线主机没有SQL实例,则返回到73秒,例如

<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: 这是我继承的DbContext类:

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? 由于73秒的超时时间太长,可以采取什么措施来加快对脱机SQL主机的检测?

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); CommandTimeout控制等待特定命令执行的时间(例如SELECT); 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. 尝试使用ConnectionTimeout看看是否有任何区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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