2013-08-23 140 views
9

我想將ConnectionTimeout設置爲默認值(15秒)以外的值。我繼承了使用的EntityFramework一些代碼,在app.config如下:使用EntityFramework時設置ConnectionTimeout

<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> 

我是誰試圖把事情的工作增加了sectino之一。我可以告訴它不工作時可以設置一個斷點:

var adapter = (IObjectContextAdapter) this; 
var objectContext = adapter.ObjectContext; 
objectContext.CommandTimeout = CommandTimeoutSeconds; 
int test = objectContext.Connection.ConnectionTimeout; 

測試始終是15.這是怎麼回事?有人可以告訴我如何設置ConnectionTimeout?我已經嘗試了「ConnectionTimeout」和「Connection Timeout」I.e.沒有空間與空間。

有人可以幫助我嗎?我正在拉我的頭髮。我相信這是一個簡單的修復! Dave

附加信息。在迴應評論,這裏是我的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; } 
} 
+0

你如何創建你的DbContext派生類?你在那裏傳遞連接字符串名嗎? – Pawel

+0

嗨帕維爾,我把DbContext驅動類放在問題中。感謝您看我的問題。 – Dave

+0

你看過http://stackoverflow.com/questions/6232633/entity-framework-timeouts嗎? –

回答

7

這是我的部分是愚蠢的,造成了問題!我把我的答案放在這裏,以防將來有人遇到這個問題。上面輸入的所有內容都是正確的,並且可以正常工作。但是,我所看到的app.config文件位於類庫(我們的DataAccess層)中。事實上,它並沒有被使用,默認的EntityFramework設置正在被使用。我很確定是什麼讓我嘗試了它,但是我將app.config設置從DataAccess圖層app.config移到了主app.config,並且所有的工作都很精美。關於我所能說的,除了我繼承的代碼以外,我可以說的是,我不清楚app.config中的值沒有被使用,也沒有調用它們或在自己的代碼中使用它們。相反,MultipleActiveResultSets和ConnectionTimeout由底層實體框架使用。

相關問題