1

I would like to know if it is possible to check FK when using SoftDelete with ASP.NET Boilerplate.

Example

Suppose these tables:

Roles: RoleId (PK) - Description
Users: UserId (PK) - Name - RoleId (FK with Roles)

Data:

Roles
1 - admin
2 - guest
Users
1 - admin - 1
2 - john - 2

So RoleId 1 should not be deleted if it was already assigned to an existing User.

Thanks in advance.

aaron
  • 39,695
  • 6
  • 46
  • 102
Seba
  • 95
  • 1
  • 10

2 Answers2

-1

Briefly your goal is non-sense. SoftDelete feature marks the record as deleted and doesn't delete it physically. It's like Recycle Bin in Windows. So that you can undelete it anytime. From the database perspective, it's relationships are consistent. Because there's the data in the table.

Solution; when you prevent users to delete an in-use record you have to validate it yourself against your business rules.

Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55
-2

Soft delete just sets a flag to mark the record as deleted.

In ABP, you can write your own checks in ApplyAbpConceptsForDeletedEntity of your DbContext:

public class AbpProjectNameDbContext // : ...
{
    // ...

    protected override void ApplyAbpConceptsForDeletedEntity(EntityEntry entry, long? userId, EntityChangeReport changeReport)
    {
        CheckForeignKeys(entry);

        base.ApplyAbpConceptsForDeletedEntity(entry, userId, changeReport);
    }

    private void CheckForeignKeys(EntityEntry entry)
    {
        var entity = entry.Entity;
        if (!(entity is ISoftDelete))
        {
            // Foreign key constraints checked by database
            return;
        }

        var role = entity as Role;
        if (role != null)
        {
            if (Users.Any(u => u.Roles.Any(r => r.RoleId == role.Id)))
            {
                throw new UserFriendlyException("Cannot delete assigned role!");
            }
        }
    }
}

Note that the template's RoleAppService actually removes users from the role before deleting it.

aaron
  • 39,695
  • 6
  • 46
  • 102