Esempio n. 1
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);
        }
Esempio n. 2
0
 public BaseRepository()
 {
     Db    = new TesteNETContext();
     DbSet = Db.Set <TEntity>();
 }