views:

22

answers:

0

I'm wondering if there is a better way to clean up orphaned records using ASP.NET and the entity framework. For my example there are three objects: Persons, PhoneNumbers and Emails. PhoneNumbers and Emails can be associated with multiple people (ie [email protected] could go to multiple people).

I want to make sure there are no orphaned PhoneNumber or Email records when I delete a Person, but the only thing I can think to do is.

var orphans = context.PhoneNumbers.Where(p => p.Persons.Count() == 0);

foreach(PhoneNumber pn in orphans)
{
  context.PhoneNumbers.DeleteObject(pn);
}

Is there a better way to do this, or do I need to check for orphaned records like this each time I delete a person?