System.OutOfMemoryException using Entity Framework?

拥有回忆 提交于 2019-11-30 03:18:51

问题


I am trying to save hundreds of thousands of records using Entity framework. After saving few hundreds of thousands of records I get following error:

:System.OutOfMemoryException

My code

  foreach (BibContent objbibcontents in lstBibContent)
        {
            db.BibContents.AddObject(objbibcontents);
            c = c + 1;
            if (c == 1000)
            {
                db.SaveChanges();
                c = 0;
            }
        }

I noticed after saving 1000 records my db is not overriding another 1000 records. it is adding them into my dbcontext.

I am creating a new instance after 1000 records but my db still has the previous object's data. See my code

   foreach (var objbibcontents in lstBibContent)
            {
                vibrantEntities db1 = new vibrantEntities(szConStr);
                lstBibCon.Add(objbibcontents);
                // db.BibContents.AddObject(objbibcontents);
                c = c + 1;
                if (c == 1000)
                {
                    foreach (BibContent bibobject in lstBibCon)
                    {
                        db1.BibContents.AddObject(bibobject);
                    }
                    lstBibCon.Clear();
                    db1.SaveChanges();
                    c = 0;
                    flag = 1;
                }
            }

回答1:


How many objects are you going to save and how big is single object? DbContext holds references to all objects you added with AddObject call. Calling SaveChanges does not purge its internal data structures so if you call your code for 1M objects you will have 1M object in memory and they will be fully alive because their root object for GC will be the context instance which is still in scope of your running code.

If you want to avoid the memory issue you should use a new context instance for every 1000 records (or even every record). The only difference between running SaveChanges for 1000 records and for single record is the automatically involved transaction.




回答2:


I search on Web and Finally i found good solution for my problem.

Fastest Way of Inserting in Entity Framework



来源:https://stackoverflow.com/questions/13954504/system-outofmemoryexception-using-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!