我遇到一个问题,即查询在检索结果之前超时。我正在使用 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/