How to delete multiple records with Entity Framewo

2019-01-21 20:08发布

问题:

I have Table like the following image:

how can I delete all records of table using Entity FrameWork based on ProjectId ?

回答1:

This one liner will do it:

  db.ProRel.RemoveRange(db.ProRel.Where(c => c.ProjectId == Project_id));


回答2:

context.Projects.Where(p => p.ProjectId == projectId)
               .ToList().ForEach(p => context.Projects.Remove(p));
context.SaveChanges();

Taken from this very similar post (which should probably be marked as duplicate).



回答3:

You can use DbSet.RemoveRange() and pass in an IEnumerable<Model>.

You build a list of models with ProjectId and pass them in RemoveRange() using the data context. Finally, call SaveChanges().