如何设置的CommandTimeout为的DbContext? [英] How to set CommandTimeout for DbContext?

查看:923
本文介绍了如何设置的CommandTimeout为的DbContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一种方法来设置的CommandTimeout进行的DbContext。搜索后,我发现通过铸造的DbContext成的ObjectContext和设置的ObjectContext的CommandTimeout属性值的方式。

I am looking a way to set CommandTimeout for DbContext. After searching I found the way by casting DbContext into ObjectContext and setting value for CommandTimeout property of objectContext.

var objectContext = (this.DbContext as IObjectContextAdapter).ObjectContext;



不过,我有一个的DbContext工作。

But I have to work with DbContext.

推荐答案

这将与你的方法工作。

It will work with your method.

或者它的子类(从的 MSDN论坛

public class YourContext : DbContext
{
  public YourContext()
    : base("YourConnectionString")
  {
    // Get the ObjectContext related to this DbContext
    var objectContext = (this as IObjectContextAdapter).ObjectContext;

    // Sets the command timeout for all the commands
    objectContext.CommandTimeout = 120;
  }
}

这篇关于如何设置的CommandTimeout为的DbContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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