我想將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; }
}
你如何創建你的DbContext派生類?你在那裏傳遞連接字符串名嗎? – Pawel
嗨帕維爾,我把DbContext驅動類放在問題中。感謝您看我的問題。 – Dave
你看過http://stackoverflow.com/questions/6232633/entity-framework-timeouts嗎? –