public virtual void InsertBulk(IEnumerable <T> entities) { using (TransactionScope scope = new TransactionScope()) { try { context.Configuration.AutoDetectChangesEnabled = false; int count = 0; foreach (var entityToInsert in entities) { ++count; context = AddToContext(context, entityToInsert, count, 100, true); } context.SaveChanges(); } finally { if (context != null) { context.Dispose(); } } scope.Complete(); } }
protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { context.Dispose(); } } this.disposed = true; }
private StoreCommEntities AddToContext(StoreCommEntities context, T entity, int count, int commitCount, bool recreateContext) { context.Set <T>().Add(entity); if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new StoreCommEntities(); context.Configuration.AutoDetectChangesEnabled = false; } } return(context); }
private StoreCommEntities UpdateToContext(StoreCommEntities context, T entity, int count, int commitCount, bool recreateContext) { context.Set <T>().Attach(entity); context.Entry(entity).State = EntityState.Modified; if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new StoreCommEntities(); context.Configuration.AutoDetectChangesEnabled = false; } } return(context); }