public void DeleteRange <TEntity>(ICollection <TEntity> entities) where TEntity : class, ITrackable, IMergeable { entities.ForEach(x => x.TrackingState = TrackingState.Deleted); foreach (var entity in entities) { Database.Remove(entity); Database.ApplyChanges(entity); } SaveChanges(); }
public static void AddOrUpdateRange <TEntity>(this DatabaseContext context, IEnumerable <TEntity> entities) where TEntity : class, ITrackable, IMergeable { var added = new Collection <TEntity>(); var updated = new Collection <TEntity>(); foreach (var entity in entities) { switch (entity.TrackingState) { case TrackingState.Added: added.Add(entity); context.Add(entity); context.ApplyChanges(entity); break; case TrackingState.Modified: var state = context.Entry(entity).State; switch (state) { case EntityState.Added: entity.TrackingState = TrackingState.Added; added.Add(entity); context.ApplyChanges(entity); break; case EntityState.Modified: case EntityState.Detached: entity.TrackingState = TrackingState.Modified; updated.Add(entity); context.ApplyChanges(entity); break; default: break; } break; case TrackingState.Unchanged: break; } } context.SaveChanges(); }