Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

c# - 在实体框架中设置数据库超时(Set database timeout in Entity Framework)

My command keeps timing out, so I need to change the default command timeout value.

(我的命令不断超时,因此我需要更改默认命令超时值。)

I've found myDb.Database.Connection.ConnectionTimeout , but it's readonly .

(我已经找到myDb.Database.Connection.ConnectionTimeout ,但是它是readonly 。)

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

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

  ask by James translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

(如果要在连接字符串中定义超时,请像下面的连接字符串中一样使用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>

Source: How to: Define the Connection String

(来源: 如何:定义连接字符串)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...