问题
I wish to know if there is a good way to do a bulk delete or delete multiple rows using the Entities Framework 4. I can't seam to find a DeleteAll command. The only one that is available is DeleteObject() which only takes one entity, I would like to perform a delete on a list of entities.Is there a better way than to loop trough the list? I did see an article that used ExecuteStoreQuery and created some sql that would perform the delete. Is there a better way than to perform any of these two options Please advice what is the best way to perform this action.
回答1:
There isn't an elegant way to do this as of yet. You're correct, you'll have to loop through the list.
This SO post has some good discussions on the topic: How do I delete multiple rows in Entity Framework (without foreach)
回答2:
I know this post is kinda of old, but a code example would be the following:
foreach(var item in items)
{
context.Remove(item);
}
来源:https://stackoverflow.com/questions/2588885/entities-framework-4-doing-a-bulk-delete