How do I delete multiple rows in Entity Framework (without foreach)?

Technology CommunityCategory: Entity FrameworkHow do I delete multiple rows in Entity Framework (without foreach)?
VietMX Staff asked 3 years ago

If you don’t want to execute SQL directly calling DeleteObject in a loop is the best you can do.

context.Widgets.Where(w => w.WidgetId == widgetId)
               .ToList().ForEach(context.Widgets.DeleteObject);
context.SaveChanges();

EntityFramework 6 has made this a bit easier with .RemoveRange().

db.People.RemoveRange(db.People.Where(x => x.State == "CA"));
db.SaveChanges();