在实体框架中设置数据库超时 [英] Set database timeout in Entity Framework

查看:145
本文介绍了在实体框架中设置数据库超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的命令保持超时。所以我需要更改默认的命令超时值。我发现myDb.Database.Connection.ConnectionTimeout,但它是只读的。



如何在Entity Framework 5中设置命令超时?

解决方案

在您的上下文中尝试这样:

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






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

 <&的ConnectionStrings GT; 

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

资料来源:如何:定义连接字符串


My command keeps timing out. so I need to change the default command timeout value. I found "myDb.Database.Connection.ConnectionTimeout" but it's read-only.

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

解决方案

Try this on your context:

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


If you want to define the timeout in the connection string, use the Connection Timeout parameter like in the following connection string:

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

Source: How to: Define the Connection String

这篇关于在实体框架中设置数据库超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆