I had the same problem and here is the solution:
Trick: when setting up the relationship between the parent and the child, you must create a “composite” key for the child. Thus, when you tell the parent to delete 1 or all of its children, the corresponding records will actually be deleted from the database.
To configure a composite key using the Fluent API:
modelBuilder.Entity<Child>.HasKey(t => new { t.ParentId, t.ChildId });
Then, to remove the related children:
var parent = _context.Parents.SingleOrDefault(p => p.ParentId == parentId);
var childToRemove = parent.Children.First();
parent.Children.Remove(childToRemove);
_context.SaveChanges();
Done!
source
share