我的命令一直超时,所以我需要更改默认的命令超时值.

我找到了myDb.Database.Connection.ConnectionTimeout个,但是readonly个.

How can I set the command timeout in Entity Framework 5 ?

推荐答案

在你的语境中试试这个:

public class MyDatabase : DbContext
{
    public MyDatabase ()
        : base(ContextHelper.CreateConnection("Connection string"), true)
    {
        ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 180; // seconds
    }
}

如果要在连接字符串中定义超时,请使用如下连接字符串中的Connection Timeout参数:

<connectionStrings>

<add name="AdventureWorksEntities"
connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
provider=System.Data.SqlClient;provider connection string='Data Source=localhost;
Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;
multipleactiveresultsets=true'" providerName="System.Data.EntityClient" />

</connectionStrings>

资料来源:How to: Define the Connection String

.net相关问答推荐

在计算Total毫秒时,.NET TimeSpan类中是否存在错误?

为什么解码后的字节数组与原始字节数组不同?

当 Func 委托需要接口作为参数时,它是如何工作的?

如何为多种文件类型设置 FileSystemWatcher 过滤器?

C# 中的批量更新

C#6.0 字符串插值本地化

在 C# DllImport 中使用 32 位或 64 位 dll

如何从 appsettings.json 中获取价值

RNGCryptoServiceProvider 的优缺点

msbuild,定义条件编译符号

如何授予所有用户对我的应用程序创建的文件的完全权限?

如何将时间设置为当天的午夜?

迭代器和枚举器的区别

关于 Enumerable.Range 与传统 for 循环的 foreach 的思考

单元测试 C# 保护方法

C# - 你如何停止计时器?

string.Empty vs null.你用哪一个?

更改 SqlConnection 超时

WPF 中的 Application.DoEvents() 在哪里?

WPF中的依赖属性和附加属性有什么区别?