public virtual int BulkDelete(Expression <Func <T, bool> > where, bool fireTriggers = true) { var entities = DataContext.Set <T>().Where(where).ToList(); if (fireTriggers) { TriggersBulk <T, DB> .RaiseDeleting(entities, DataContext); } var bulkDelete = DataContext.Set <T>().Where(where).Delete(); if (fireTriggers) { TriggersBulk <T, DB> .RaiseDeleted(entities, DataContext); } return(bulkDelete); }
/// <summary> /// Bulk Delete Entities in DB /// </summary> /// <typeparam name="DB">DB Context Type</typeparam> /// <typeparam name="T">Entity Type</typeparam> /// <param name="dbContext">DB Context</param> /// <param name="entities">List of Entities</param> /// <param name="identificator"> /// Expression which generate unique value for each entity. It can be one column or combination /// of columns /// </param> /// <param name="configAction">Configure Bulk Delete Options</param> /// <returns></returns> public static async Task <int> BulkDeleteAsync <DB, T>(this DB dbContext, IEnumerable <T> entities, Expression <Func <T, object> > identificator, Action <BulkDeleteOptions <T> > configAction = null) where DB : DbContext where T : class { var enumerable = entities.ToList(); if (!enumerable.Any()) { return(0); } var options = new BulkDeleteOptions <T>(); configAction?.Invoke(options); if (options.FireTriggers) { TriggersBulk <T, DB> .RaiseDeleting(enumerable, dbContext, identificator); } var bulkDelete = new BulkOperations().Setup <T>() .ForCollection(enumerable) .WithTable(dbContext.GetTableName <T>()) .WithBulkCopySettings(new BulkCopySettings() { BatchSize = options.BatchSize, BulkCopyTimeout = options.Timeout, SqlBulkCopyOptions = options.SqlBulkCopyOptions }) .AddColumn(identificator) .BulkDelete(); var num = await bulkDelete .MatchTargetOn(identificator) .CommitAsync(dbContext.Database.Connection as SqlConnection); if (options.FireTriggers) { TriggersBulk <T, DB> .RaiseDeleted(enumerable, dbContext); } return(num); }