Entities Framework 4 doing a bulk delete

风格不统一 提交于 2020-01-13 08:48:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!