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

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

这些下面的代码给出了我的 Rehber 数据的全部数据。但是,如果我想通过 Gridview 显示网页,请向我发送内存不足异常错误。

基因提示.BAL:



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

如果我像那样直接绑定(bind)数据虚拟 gridview 就不会出现任何问题,一切都很好!!!

 <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 özel durum oluş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(对象状态图)+183 System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Serialize(对象状态)+4 System.Web.UI.Util.SerializeWithAssert(IStateFormatter 格式化程序,对象状态图)+37 System.Web.UI.HiddenFieldPageStatePersister.Save() +79 System.Web.UI.Page.SavePageStateToPersistenceMedium(对象状态)+105 系统.Web.UI.Page.SaveAllState() +236 System.Web.UI.Page.ProcessRequestMain( bool 值 includeStagesBeforeAsyncPoint, bool 值 includeStagesAfterAsyncPoint)+1099

最佳答案

所以看起来问题是你在状态中投入了太多;这很可能意味着以下一项(或两项):

  • 您阅读的行太多了
  • 序列化程序遍历(也许是延迟加载?)属性,导致加载/序列化的数据多于您的预期

为了解决这个问题,我建议在执行任何可能将数据放入状态之前先投影到一个简单的 DTO 模型(这样您就可以确切地知道您正在序列化的数据),并仔细查看关于您正在处理多少行(适本地使用 TakeWhere)。

关于c# - 如何解决 Entity Framework 中的内存不足异常错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2577477/

相关文章:

c# - 不包含 'WhenAll' Mvc 4 .Net framework 4.0 的定义

c# - OPOS PosExplorer.GetDevice() 在 Windows 服务中执行时返回 null

C#-WPF 奇怪的异步行为

c# - 在 DllImport 中指定 Charset.Unicode 是否分配字符串?

c# - 类型参数约束 - 没有泛型(或最近的报价!)

c# - 将 Sphero 2.0 连接到 Mac OSX 10.9

c# - 无效的组名 : Group names must begin with a word character

c# - 根据 Visual Studio 2010,jQuery 函数 "undefined"

asp.net - Azure SQL 标准异地复制和灾难恢复

c# - Blazor 页面中的 CancellationToken?