Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.OutOfMemoryException using Entity Framework?

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;
                }
            }
like image 807
Kaps Hasija Avatar asked Dec 19 '12 14:12

Kaps Hasija


1 Answers

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

Fastest Way of Inserting in Entity Framework

like image 52
Kaps Hasija Avatar answered Oct 08 '22 18:10

Kaps Hasija