如何解决Entity Framework中的内存不足异常错误?

时间:2010-04-05 08:17:18

标签: c# .net asp.net visual-studio-2008 entity-framework

以下代码提供了我的Rehber数据的完整数据。但是,如果我想通过Gridview显示网页,请发送内存异常错误。

GenoTip.BAL:



    public static List<Rehber> GetAllDataOfRehber()
        {
            using (GenoTipSatisEntities genSatisCtx = new GenoTipSatisEntities())
            {
                ObjectQuery<Rehber> rehber = genSatisCtx.Rehber;
                return rehber.ToList();
            }
        }

如果我直接绑定数据网格像这样没有问题发生每件事都很棒!!!

 <asp:GridView ID="gwRehber" runat="server">

       </asp:GridView>

如果以上代码将数据发送到Satis.aspx页面:

using GenoTip.BAL;


namespace GenoTip.Web.ContentPages.Satis
{
    public partial class Satis : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gwRehber.DataSource = SatisServices.GetAllDataOfRehber();
                gwRehber.DataBind();
                //gwRehber.Columns[0].Visible = false;
            }
        }
    }
}

但我重新安排了我的gridview发送我的内存异常!!!!我需要这个安排来展示详细信息!!!

 <asp:GridView ID="gwRehber" runat="server">
       <Columns>
      <%-- <asp:TemplateField>
       <ItemTemplate>
        <asp:Button runat="server" ID="btnID" CommandName="select"  CommandArgument='<%# Eval("ID") %>'  Text="Seç" />
       </ItemTemplate>

       </asp:TemplateField>--%>
       <asp:BoundField DataField="Ad" HeaderText="Ad" />
       <asp:BoundField DataField="BireyID" HeaderText="BireyID" Visible="false" />
       <asp:BoundField DataField="Degistiren" HeaderText="Değiştiren" />
        <asp:BoundField DataField="EklemeTarihi" HeaderText="EklemeTarihi" />
       <asp:BoundField DataField="DegistirmeTarihi" HeaderText="Değiştirme Tarihi" Visible="false" />
       <asp:BoundField DataField="Ekleyen" HeaderText="Ekleyen" />
        <asp:BoundField DataField="ID" HeaderText="ID" Visible="false" />
       <asp:BoundField DataField="Imza" HeaderText="Imza" />
        <asp:BoundField DataField="KurumID" HeaderText="KurumID" Visible="false" />
       </Columns>
       </asp:GridView>

错误明细:

[OutOfMemoryException:'System.OutOfMemoryException'türündeözeldurumoluşturuldu。]    System.String.GetStringForStringBuilder(String value,Int32 startIndex,Int32 length,Int32 capacity)+29    System.Convert.ToBase64String(Byte [] inArray,Int32 offset,Int32 length,Base64FormattingOptions options)+146    System.Web.UI.ObjectStateFormatter.Serialize(Object stateGraph)+183    System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Serialize(Object state)+4    System.Web.UI.Util.SerializeWithAssert(IStateFormatter formatter,Object stateGraph)+37    System.Web.UI.HiddenFieldPageStatePersister.Save()+79    System.Web.UI.Page.SavePageStateToPersistenceMedium(Object state)+105    System.Web.UI.Page.SaveAllState()+236    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+1099

1 个答案:

答案 0 :(得分:2)

所以看起来问题就是你把太多的东西放进了州;这很可能意味着一个(或两个):

  • 你读得太多,行太多了
  • 序列化程序正在处理(可能是延迟加载的?)属性,导致的数据超出预期加载/序列化的数据

为了解决这个问题,我建议在做任何可以将数据置于状态的事情之前投射到简单的DTO模型(因此你知道正好你正在序列化什么数据),并仔细观察关于您要处理的行数(适当地使用TakeWhere)。