Reputation: 11
Is it possible to update/remove multiples entities, for example
[HttpPost, ActionName("removeresponsible")]
public ActionResult removeresponsibleaction(Responsible modelo)
{
Responsible responsible = db.Responsibles.Single(x => x.responsible_id == modelo.responsible_id);
db.Responsibles.Remove(responsible);
db.SaveChanges();
ViewBag.responsible = db.Responsibles.ToList();
return View("responsiblemanager");
}
This code works with one single responsible with an unique id, but how can I do it if there are many responsible with the same id? For example, I know that with SQL it would be something like this "delete from table where responsible_id=3".
Upvotes: 1
Views: 267
Reputation: 2522
If you aren't using EF 6, you will have to loop through your list of models, and delete each one separately.
e.g.
[HttpPost, ActionName("removeresponsible")]
public ActionResult removeresponsibleaction(IEnumerable<Responsible> models)
{
foreach(var model in models)
{
Responsible responsible = db.Responsibles.Single(x => x.responsible_id == model.responsible_id);
db.Responsibles.Remove(responsible);
}
db.SaveChanges();
ViewBag.responsible = db.Responsibles.ToList();
return View("responsiblemanager");
}
Upvotes: 0
Reputation: 101711
If you are using Entity Framework 6
you can use RemoveRange
method:
var responsibles = db.Responsibles.Where(x => x.responsible_id == modelo.responsible_id).ToList();
db.Responsibles.RemoveRange(responsibles);
Upvotes: 1