I have Table like the following image:
how can I delete all records of table using Entity FrameWork based on ProjectId ?
I have Table like the following image:
how can I delete all records of table using Entity FrameWork based on ProjectId ?
This one liner will do it:
db.ProRel.RemoveRange(db.ProRel.Where(c => c.ProjectId == Project_id));
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).
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()
.