I've been using NHibernate for quite some time now, but as of late have been struggling with one little aspect regarding deletes. Lets take the contact - address example, it's not ideal, but it'll do. An address has a foreign key to contact and a contact has no notion of addresses.
When we would like to delete a contact, it's crucial that no contact can be deleted when they have an address assigned. But how would one go about checking this? I could before deleting the contact check using the address repository if there's any address assigned to the contact, but this doesn't really feel right, we'd like to add extra classes with foreign keys to contact and we would have to update this logic every time.
The crucial part here is that the relation is known in one direction only and cascading deletes isn't an option. I don't want to add addresses to a contact either, which would make the checking very straightforward though.
Could there be a kind of generic method to check for foreign key conflicts when deleting an entity? Wrapping the delete in a try-catch block would do the trick, but that's just wrong.
Thanks in advance.
|