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); }
public void Deatach(TEntity obj) { Db.Entry(obj).State = EntityState.Detached; }