【发布时间】:2014-05-01 18:55:39
【问题描述】:
我有一个现有项目,我想将 ObjectDataSource 用于大型数据集,该数据集对 DevExpress 网格的结果进行分页。
代码示例如下所示。
我在 ObjectDataSource 所需的静态方法中使用实体框架上下文时遇到问题。
我的问题是我得到了这个异常:
System.ObjectDisposedException:ObjectContext 实例已被 处置,不能再用于需要一个操作 连接。
有谁知道我可以如何更改以下内容以支持将 Entities 实例从 Page 传递到静态方法?
这是ObjectDataSource的例子
<dx:ASPxGridView ID="DefinedReportGrid" runat="server" EnableTheming="True" Theme="Office2010Blue" EnableViewState="False"
ClientInstanceName="DefinedReportGrid" DataSourceForceStandardPaging="True" DataSourceID="ReportDataSource">
<SettingsPager PageSize="30"></SettingsPager>
</dx:ASPxGridView>
<asp:ObjectDataSource ID="ReportDataSource" runat="server" EnablePaging="True"
StartRowIndexParameterName="startRecord" MaximumRowsParameterName="maxRecords" SelectCountMethod="GetPageCount" SelectMethod="GetData"
TypeName="ReportService">
</asp:ObjectDataSource>
这是我的带有静态方法的服务类,ObjectDataSource 可以使用这些方法
public class ReportService
{
[DataObjectMethod(DataObjectMethodType.Select, true)]
public static DataTable GetData(int startRecord, int maxRecords)
{
// Use EF DbContent and LINQ Query to fetch paged data, and return a DataTable
// The data is PIVOTED in the code before returning it as a DataTable
return outputTable;
}
public static List<int> GetWeeks()
{
// This also requires use of the EF DbContent
...
}
public static int GetPageCount()
{
// This also requires use of the EF DbContent
// Used to count the full data for applying the correct total Page Count
...
}
}
这是网页表单页面的代码
public partial class DefinedReport : System.Web.UI.Page
{
private Entities _modelContext = ((Global)HttpContext.Current.ApplicationInstance).Entities;
protected void Page_Init(object sender, EventArgs e)
{
...
}
protected void Page_Load(object sender, EventArgs e)
{
...
}
}
这是全局代码,在请求开始和结束时设置实体上下文。
public class Global : HttpApplication
{
public Entities Entities { get; set; }
...
private void Application_BeginRequest(object sender, EventArgs e)
{
Entities = new Entities();
}
private void Application_EndRequest(object sender, EventArgs e)
{
if (Entities != null)
{
Entities.Dispose();
}
}
}
【问题讨论】:
标签: c# entity-framework webforms devexpress objectdatasource