Ejemplo n.º 1
0
        public void UpdateWithTransaction(List <TEntity> lstUpdate)
        {
            TesteNETContext context = null;

            try
            {
                context = new TesteNETContext();
                context.Configuration.AutoDetectChangesEnabled = false;

                var lstDistinct = lstUpdate.Distinct().ToList();

                int count = 0;
                foreach (var entityToUpdate in lstDistinct)
                {
                    ++count;
                    context = AddToContextUpdate(context, entityToUpdate, count, 200, false);
                }

                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                                                       validationErrors.Entry.Entity.ToString(),
                                                       validationError.ErrorMessage);

                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }
            catch (Exception sex)
            {
                throw sex;
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        public TesteNETContext AddToContextUpdate(TesteNETContext context, TEntity entity, int count, int commitCount, bool recreateContext)
        {
            try
            {
                var entry = context.Entry(entity);

                context.Set <TEntity>().Attach(entity);
                entry.State = EntityState.Modified;

                if (count % commitCount == 0)
                {
                    context.SaveChanges();
                    if (recreateContext)
                    {
                        context.Dispose();
                        context = new Context.TesteNETContext();
                        context.Configuration.AutoDetectChangesEnabled = true;
                    }
                }
            }
            catch (OptimisticConcurrencyException)
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                                                       validationErrors.Entry.Entity.ToString(),
                                                       validationError.ErrorMessage);

                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(context);
        }
Ejemplo n.º 3
0
 public void Dispose()
 {
     Db.Dispose();
     GC.SuppressFinalize(this);
 }