Simple delete query using EF Code First

jhoefnagels picture jhoefnagels · Mar 6, 2012 · Viewed 12k times · Source

Is there any way to accomplsig something simple as this using EF Code First

DELETE FROM Order WHERE OrderDate >= @minOrderDate AND OrderDate >= @maxOrderDate

I have a table of which I would like to delete at least 10.000 records. I think it would be rather inefficient to retrieve all records first before I can delete them using a for-each loop.

Answer

Eric J. picture Eric J. · Mar 19, 2015

You can use

ctx.Database.ExecuteSqlCommand(sqlDeleteStatement,
    new SqlParameter("@minOrderDate", minDate),
    new SqlParameter("@maxOrderDate", maxDate));

NOTE: The accepted answer does not compile.