Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Check Referential Integrity in Soft Delete #1641


User avatar
0
maharatha created

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)
  • User Avatar
    0
    ismcagdas created
    Support Team

    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).

  • User Avatar
    0
    maharatha created

    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.

  • User Avatar
    0
    ismcagdas created
    Support Team

    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.