Entity Framework Pitfalls: Deleting Orphans
The idea for this post came from Jimmy Bogard, of AutoMapper fame.
Entity Framework does not really have the notion of parents and children, only of relations. Some of these relations can be mandatory, others can be optional. It is possible to specify cascade delete rules, so that when an entity is removed from the database, all entities that depend on it are also removed. However, what happens if we remove a related (child) entity from it’s parent’s children collection? Consider this code:
1: using (var ctx = new OrdersContext())
2: {
3: var customer = ctx.Customers.First();
4: customer.Orders.Remove(customer.Orders.First());
5:
6: ctx.SaveChanges();
7: }
What we are doing here is removing the association between an Order and its Customer. Because an Order cannot exist without a Customer, we are turning it into an orphan. It is basically the same as:
1: var order = ctx.Orders.First();
2: order.Customer = null;
Do note that we are not deleting anything, just removing the related (parent) reference. In this case, Entity Framework will, of course, fail, complaining about the missing required relation.
Entity Framework has really no workaround for this – at least, not that I know of – so you have to delete the child entities explicitly:
1: var customer = ctx.Customers.First();
2: var order = customer.Orders.First();
3:
4: customer.Orders.Remove(order);
5: ctx.Orders.Remove(order);
6:
7: ctx.SaveChanges();
As a side note, NHibernate handles this very well, by setting the cascade option to delete-orphan.