Пример #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();
                }
            }
        }
Пример #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);
        }
Пример #3
0
        public void AddWithTransactionPlus(List <TEntity> lstModel)
        {
            try
            {
                Db.Configuration.AutoDetectChangesEnabled = false;

                int count = 0;
                foreach (var entityToInsert in lstModel)
                {
                    ++count;
                    Db = AddToContext(Db, entityToInsert, count, 100, false);
                }

                Db.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;
            }
            finally
            {
                Db.Configuration.AutoDetectChangesEnabled = true;
            }
        }
Пример #4
0
 public BaseRepository()
 {
     Db    = new TesteNETContext();
     DbSet = Db.Set <TEntity>();
 }