How to remove child one to many related records in EF code first database?
Solution 1:
In EF6 a faster way to do the operation is...
context.Children.RemoveRange(parent.Children)
Solution 2:
Cascading delete has no effect here because you don't delete the parent
but just call InsertOrUpdate
. The correct procedure is to delete the children one-by-one, like so for example:
using (var context = new MyContext())
{
var parent = context.Parents.Include(p => p.Children)
.SingleOrDefault(p => p.Id == parentId);
foreach (var child in parent.Children.ToList())
context.Children.Remove(child);
context.SaveChanges();
}