Moderator71
Moderator71

Reputation: 395

How to perform a bulk delete with the Entity Framework?

How do I delete multiple entities without a loop? Currently, I have:

 Dim itemsToDelete As List(Of Item) = (From t In _entities.Item _
                                            Where t.Column = columnValue).ToList

 For Each item In itemsToDelete
      _entities.DeleteObject(item)
 Next

 _entities.SaveChanges()

Upvotes: 1

Views: 1116

Answers (1)

marc_s
marc_s

Reputation: 754268

One word: DON'T !

Any of the typical ORM's - be it Linq-to-SQL, NHibernate, Entit Framework and any other - are great at handling single or a few objects.

There are not however designed or optimized for bulk handling.

If you need to delete hundreds or thousands of rows: use straight SQL - either as an ad-hoc SQL query, or as a stored procedure. It's much easier and much more efficient to do it that way.

Upvotes: 3

Related Questions