using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace GreenTree.Strohrmann.ERP.Domain.Extension { public static class DbSetExtension { /// /// Removes a sequence of entities from a specific DbSet filtered by a predicate /// /// Entity type. /// Database DbSet. /// Filter expression. public static void RemoveWhere(this DbSet dbSet, Expression> expression) where TEntity : class { var entities = dbSet .Where(expression); dbSet.RemoveRange(entities); } /// /// Removes a sequence of entities from a specific DbSet filtered by a predicate and inserts a set of new entities for them /// /// Entity type. /// Database DbSet. /// Filter expression. /// New entities. public static void ReplaceWhere(this DbSet dbSet, Expression> expression, IEnumerable newEntities) where TEntity : class { RemoveWhere(dbSet, expression); dbSet.AddRange(newEntities); } } }