コード例 #1
0
        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();
            }
        }
コード例 #2
0
 protected virtual void Dispose(bool disposing)
 {
     if (!this.disposed)
     {
         if (disposing)
         {
             context.Dispose();
         }
     }
     this.disposed = true;
 }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }