Bulk deleting rows with RemoveRange()

c#, entity-framework, entity-framework-6

Solution

I think we reached here a limitation of EF. Sometimes you just have to use ExecuteSqlCommand to stay performant.

Problem

I am trying to delete multiple rows from a table. In regular SQL Server, this would be simple as this: ``` DELETE FROM Table WHERE Table.Column = 'SomeRandomValue' AND Table.Column2 = 'AnotherRandomValue' ``` In Entity Framework 6, they have introduced RemoveRange() method. However, when I use it, rather than deleting rows using the where clauses that I provided, Entity Framework queries the database to get all rows that match the where clauses and delete them one by one using their primary keys. Is this the current limitation of EntityFramework? Or am I using `RemoveRange()` wrong? Following is how I am using `RemoveRange()`: ``` db.Tables.RemoveRange( db.Tables .Where(_ => _.Column == 'SomeRandomValue' && _.Column2 == 'AnotherRandomValue') ); ```

Original source

Related problems