Simple removal request using EF Code First

Is there a way to do something simple like this when using EF Code First

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

I have a table from which I would like to delete at least 10,000 records. I think it would be pretty inefficient to fetch all the records first before I can delete them using the for-each loop.

+4
source share
2 answers

You can always send through source SQL through your context. _context.Database.SqlQuery(sqlDeleteStatement, parameterList)

 string sqlDeleteStatement = "DELETE FROM Order" + "WHERE OrderDate >= @minOrderDate AND OrderDate >= @maxOrderDate"; List<SqlParameter> parameterList = new List<SqlParameter>(); parameterList.Add(new SqlParameter("@minOrderDate", minDate)); parameterList.Add(new SqlParameter("@maxOrderDate", maxDate)); _context.Database.SqlQuery(sqlDeleteStatement, parameterList); 
+3
source

you can use

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

NOTE: the accepted answer does not compile.

+16
source

Source: https://habr.com/ru/post/1399989/


All Articles