c# - System.Data.Services.Client.DataServiceContext 超时属性不起作用

标签 c# entity-framework

我遇到一个问题,即查询在检索结果之前超时。我正在使用 Entity Framework v.6.0。设置 context.Timeout 属性似乎没有任何影响。

这是类声明和构造函数,因此您知道我正在处理什么。它是从 Reference.cs 生成的代码:

    public partial class PSIDevEntities : global::System.Data.Services.Client.DataServiceContext
{
    /// <summary>
    /// Initialize a new PSIDevEntities object.
    /// </summary>
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
    public PSIDevEntities(global::System.Uri serviceRoot) : 
            base(serviceRoot, global::System.Data.Services.Common.DataServiceProtocolVersion.V3)
    {
        this.ResolveName = new global::System.Func<global::System.Type, string>(this.ResolveNameFromType);
        this.ResolveType = new global::System.Func<string, global::System.Type>(this.ResolveTypeFromName);
        this.OnContextCreated();
        this.Format.LoadServiceModel = GeneratedEdmModel.GetInstance;
    }

我尝试将超时设置为较高的数字。但是,查询在 30 秒后超时。

var context = new PSIDevEntities(URI);
context.Timeout = 120000;
var result = from p in context.vwPacings
             where p.InactiveDateTime == null
             orderby p.ProgramTitle
             select p;
// timeout occurs here after 30 seconds
retList = result.ToList();

某处有 30 秒的默认超时。我需要做什么来增加此查询的超时时间?

最佳答案

答案是修改数据服务中自动生成的代码。哎呀!但它有效。更改数据服务的命令超时。将这两行输入到 DbContext 的构造函数中:

        var objectContext = (this as IObjectContextAdapter).ObjectContext;
        objectContext.CommandTimeout = 120;   // Or any required timeout

因此,对于我的修复,我的构造函数现在如下所示:

public partial class PSIDevEntities : DbContext
{
    public PSIDevEntities()
        : base("name=PSIDevEntities")
    {
        base.Configuration.ProxyCreationEnabled = false;
        var objectContext = (this as IObjectContextAdapter).ObjectContext;
        objectContext.CommandTimeout = 120;
    }
...

关于c# - System.Data.Services.Client.DataServiceContext 超时属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63252685/

相关文章:

c# - 在读取时使用来自 Azure DownloadToStreamAsync 的字节?

c# - 在 C# 中使用不强制转换的整数枚举

c# - openTk .net 核心保持窗口始终在顶部

entity-framework - Entity Framework (ASP.NET): How do you update the column mapping after adding a column to the framework?

c# - 使用表达式在 EF4 中渴望加载多对多?

entity-framework - 使用 GetAllInclusion 在一次调用中获取多个级别

WPF 绑定(bind)到 Entity Framework 不更新

c# - 使用实体服务/存储库模式时,在模型中引用 System.Web.Security 是一种不好的做法吗?

c# - Ninject:通过属性进行拦截,而不从 InterceptAttribute 派生

c# - 应该在通用类字段中使用 default(T) 或...将其初始化为默认值?