Hi -
Is there a way I can check the referential integrity while deleting items ? e.g. I want to restrict Tenants to be deleted if any transaction or user is associated with that Tenant.
How do i do it ?
3 Answer(s)
-
0
Hi,
If your case is related to data loss, since Tenant entity implements ISoftDelete it's not deleted actually, just marked as deleted.
You can also do it by adding a foreign key and disable cascade delete, but this approach does not work with database per tenant architecture. Because tenant's are stored in a different database than tenant records (users for example).
-
0
Probably Tenant was a bad example. I am not worried about data loss as I understand we use SoftDelete pattern. But I think it's good to throw a user friendly message if we are trying to delete a row in a table which is being used as a foreign key in another table.
-
0
If none of those entities implements ISoftDelete, you can disable cascade delete and get an exception if there is a foreign key in another table. Then you can show a message to user.
But if you want to do it with entities which implement ISoftDelete, you have to manually handle it. Because in that case SQL Server will not throw an exception for you. It's a nice feature but hard to implement.
Maybe you can create a common EntityUpdated event and in that event check if entity implements ISoftDelete and it's IsDeleted field is true. If it is, then you can check related entities which are not deleted.
But this might be a heavy process.