Entity structure persists for a long time

There are many EF articles on EF, but I have looked through them and used their answers and still seem to be getting very slow results.

My code looks like this:

using (MarketingEntities1 db = new MarketingEntities1())
        {
            //using (var trans = db.Database.BeginTransaction(IsolationLevel.ReadUncommitted))
            //{
                int count = 0;
                db.Configuration.AutoDetectChangesEnabled = false;
                db.Configuration.ValidateOnSaveEnabled = false;
                while (count < ranges.Count)
                {
                    if (bgw != null)
                    {
                        bgw.ReportProgress(0, "Saving count: " + count.ToString());
                    }
                    db.Set<xGeoIPRanx>().AddRange(ranges.Skip(count).Take(BATCHCOUNT));
                    db.SaveChanges();
                    count+=BATCHCOUNT;

                }
                //trans.Commit();
            //}
        }

      

Each batch takes 30 seconds. BatchCount is 1000. I know EF is not that slow. You can see that I stopped using the transaction, I tracked it down, none of this helped.

Additional Information:

xGeoIpRanx is an empty table with no PK (I'm not sure how much this helps). I am trying to insert about 10 mil.

Edit:

I feel stupid but I am trying to use bulkInsert and I keep getting this object, no error exists, I am looking at this code

using (var ctx = GetContext())
{
  using (var transactionScope = new TransactionScope())
  {
    // some stuff in dbcontext

    ctx.BulkInsert(entities);

    ctx.SaveChanges();
    transactionScope.Complete();
  }
}

      

What are "entities" I tried a list of my objects that don't work, what datatype?

nvm works as expected, it was a strange error due to the way I created the edmx file

+3


source to share


1 answer


Suspend the debugger 10 times under load and look at the stack including external code. Where does he stay most often?

...

.SaveChanges () takes a long time. just from some quick tests, ADO.net code



This means network latency and server runtime are causing this. For attachments, the server runtime is usually not that long. You can't do anything about network latency with EF, as it sends one batch per insert. (Yes, that's a lack of a frame.)

Don't use EF for bulk work. With comments, use table-value parameters or SqlBulkCopy

or any other bulk inserter such as Aducci's suggestion .

+4


source







All Articles