0

我想在 EF 中运行一个存储过程。我首先将过程添加到模型中并设置返回值。生成此代码:

public ObjectResult<Report3> SPSelectReport3(global::System.String stringWhereParameter, Nullable<global::System.Int32> pageIndex, Nullable<global::System.Int32> pageSize)
{
    ObjectParameter stringWhereParameterParameter;
    if (stringWhereParameter != null)
    {
        stringWhereParameterParameter = new ObjectParameter("StringWhereParameter", stringWhereParameter);
    }
    else
    {
        stringWhereParameterParameter = new ObjectParameter("StringWhereParameter", typeof(global::System.String));
    }

    ObjectParameter pageIndexParameter;
    if (pageIndex.HasValue)
    {
        pageIndexParameter = new ObjectParameter("PageIndex", pageIndex);
    }
    else
    {
        pageIndexParameter = new ObjectParameter("PageIndex", typeof(global::System.Int32));
    }

    ObjectParameter pageSizeParameter;
    if (pageSize.HasValue)
    {
        pageSizeParameter = new ObjectParameter("PageSize", pageSize);
    }
    else
    {
        pageSizeParameter = new ObjectParameter("PageSize", typeof(global::System.Int32));
    }
    // ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 999999;
    return base.ExecuteFunction<Report3>("SPSelectReport3", stringWhereParameterParameter, pageIndexParameter, pageSizeParameter);
}
/// <summary>
/// No Metadata Documentation available.
/// </summary>
/// <param name="mergeOption"></param>
/// <param name="stringWhereParameter">No Metadata Documentation available.</param>
/// <param name="pageIndex">No Metadata Documentation available.</param>
/// <param name="pageSize">No Metadata Documentation available.</param>
public ObjectResult<Report3> SPSelectReport3(global::System.String stringWhereParameter, Nullable<global::System.Int32> pageIndex, Nullable<global::System.Int32> pageSize, MergeOption mergeOption)
{
    ObjectParameter stringWhereParameterParameter;
    if (stringWhereParameter != null)
    {
        stringWhereParameterParameter = new ObjectParameter("StringWhereParameter", stringWhereParameter);
    }
    else
    {
        stringWhereParameterParameter = new ObjectParameter("StringWhereParameter", typeof(global::System.String));
    }

    ObjectParameter pageIndexParameter;
    if (pageIndex.HasValue)
    {
        pageIndexParameter = new ObjectParameter("PageIndex", pageIndex);
    }
    else
    {
        pageIndexParameter = new ObjectParameter("PageIndex", typeof(global::System.Int32));
    }

    ObjectParameter pageSizeParameter;
    if (pageSize.HasValue)
    {
        pageSizeParameter = new ObjectParameter("PageSize", pageSize);
    }
    else
    {
        pageSizeParameter = new ObjectParameter("PageSize", typeof(global::System.Int32));
    }

    //  ((IObjectContextAdapter)).ObjectContext.CommandTimeout = 180;
    return base.ExecuteFunction<Report3>("SPSelectReport3", mergeOption, stringWhereParameterParameter, pageIndexParameter, pageSizeParameter);
}

但是当我运行代码时,我得到一个超时错误。如何在此代码中设置超时?

4

1 回答 1

0

使用 ObjectContext 的 CommandTimeout 属性

例如

using (var db = new MyEFEntities())
{
     string whereClause = "Id = 12345";
     db.CommandTimeout = 180; //in seconds, ideally use a value from your config file e.g. Convert.ToInt32(ConfigurationManager.AppSettings["DbCommandTimeout"]);
     var retval = db.SPSelectReport3(whereClause);
     //do something here with results
}
于 2013-05-07T11:20:05.693 回答