Missing EF Feature Workarounds: Cascade delete orphans
Currently, Entity Framework cannot cascade delete orphans. In fact, there’s not a real concept of parent-child relationships, there’s only navigation properties, collection properties, and a notion of required/optional relationships. You can cascade delete if a parent is deleted, but if you try something like this:
You’ll have an orphaned address! In a parent-child relationship, you’d like the parent to completely own the relationship to the child. Children cannot live on their own, so even if you configure EF to have a required relationship from child to parent:
You’ll get an error on SaveChanges about a required relationship not being available. What we’d like to do is when true child entities get detached from their parent, they get deleted. There’s no way to do this in the entity mapping, but we can override SaveChanges in our custom DbContext class to detect orphans and then delete them:
Not terrible, but it preserves the encapsulation of my parent-child relationship from Customer and Address.