使用EntityFramework时设置ConnectionTimeout

Dav*_*ave 9 connection entity-framework connection-string

我想将ConnectionTimeout设置为默认值以外的值,即15秒.我继承了一些使用EntityFramework的代码,app.config看起来像这样:

<configuration>
   <configSections>
      <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS; Integrated Security=True; ConnectionTimeout=30; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; ConnectionTimeout=30; MultipleActiveResultSets=True" />
  </parameters>
</defaultConnectionFactory>
</entityFramework>
Run Code Online (Sandbox Code Playgroud)

我是那个为了让事情发挥作用而添加了sectino的人.我可以告诉它在设置断点时无效:

var adapter = (IObjectContextAdapter) this;
var objectContext = adapter.ObjectContext;
objectContext.CommandTimeout = CommandTimeoutSeconds;
int test = objectContext.Connection.ConnectionTimeout;
Run Code Online (Sandbox Code Playgroud)

测试始终是15.发生了什么?有人能告诉我如何设置ConnectionTimeout吗?我已经尝试过"ConnectionTimeout"和"Connection Timeout",即没有空间与空间.

有人能帮我吗?我把头发拉了出来.我确定这是一个简单的修复!戴夫

附加信息.回应评论,这是我的DbContext派生类...

public class SessionDataContext : DbContext
{
    // Command timeout (seconds)
    private const int CommandTimeoutSeconds = 30;

    /// <summary>
    /// Constructor that takes db name.
    /// The connection string and db itself is configured in the this project's app.config file
    /// </summary>
    /// <param name="dbName"></param>
    public SessionDataContext(string dbName) : base(dbName)
    {
        Database.SetInitializer(new SessionDataContextInitializer());

        // Set timeout (based on code from http://stackoverflow.com/questions/6232633/entity-framework-timeouts)
        var adapter = (IObjectContextAdapter) this;
        var objectContext = adapter.ObjectContext;
        objectContext.CommandTimeout = CommandTimeoutSeconds;
        int test = objectContext.Connection.ConnectionTimeout;
    }

    /// <summary>
    /// Session table's records
    /// </summary>
    public DbSet<Session> Sessions { get; set; }

    /// <summary>
    /// SessionType table's records
    /// </summary>
    public DbSet<SessionType> SessionTypes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ave 7

我个人的愚蠢导致了这个问题!我把答案放在这里以防将来有人遇到这个问题.我输入的所有内容都是正确的,并且可以正常工作.但是,我正在查看的app.config文件位于类库(我们的DataAccess层)中.事实上,它根本没有被使用,并且正在使用默认的EntityFramework设置.我确定是什么导致我尝试它,但我将app.config设置从DataAccess层app.config移动到主app.config并且所有工作都非常好.除了我继承代码之外,我可以在我的辩护中说的是,我不清楚app.config中的值是否未被使用,并且没有调用它们或在自己的代码中使用它们.相反,MultipleActiveResultSets和ConnectionTimeout由底层实体框架使用.