2013-08-23 21 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を追加したものをです。何が起こっているかを常に15

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

テストされています。私が言うことができる、にブレークポイントを設定することが動作しないのですか? 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; } 
} 
+0

DbContext派生クラスを作成するにはどうすればよいですか?そこに接続文字列名を渡していますか? – Pawel

+0

こんにちはPawel、私はDbContext drivedクラスを質問に入れました。私の質問を見てくれてありがとう。 – 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は、基礎となるEntity Frameworkによって使用されます。

関連する問題