Many-to-many with class for join entity EF
I'm facing an issue with implementing soft deletes in a custom many-to-many relationship class. I've created a custom class to handle the relationship, but I'm struggling to figure out how to modify the repository's get method to only fetch entities where the relationship's DeletedAt is not null.
public class Role : EntityBase
{
public required string Name { get; set; }
public string? Description { get; set; }
public ICollection<User> Users { get; set; } = [];
public ICollection<Permission> Permissions { get; set; } = [];
public ICollection<RolePermission> RolePermissions { get; set; } = [];
public ICollection<UserRole> UserRoles { get; set; } = [];
}
public class Permission : EntityBase
{
public required string Name { get; set; }
public string? Description { get; set; }
public ICollection<Role> Roles { get; set; } = [];
public ICollection<RolePermission> RolePermissions { get; set; } = [];
}
public class RolePermission : EntityBase
{
public required Guid RoleUuid { get; set; }
public Role? Role { get; set; }
public required Guid PermissionUuid { get; set; }
public Permission? Permission { get; set; }
}
public async Task<Role?> GetActiveWithPermissionByIdAsync(Guid uuid)
{
return await _context.Roles
.AsNoTracking()
.Include(r => r.Permissions.Where(p => p.DeletedAt == null))
.FirstOrDefaultAsync(r => r.Uuid == uuid && r.DeletedAt == null);
}public class Role : EntityBase
{
public required string Name { get; set; }
public string? Description { get; set; }
public ICollection<User> Users { get; set; } = [];
public ICollection<Permission> Permissions { get; set; } = [];
public ICollection<RolePermission> RolePermissions { get; set; } = [];
public ICollection<UserRole> UserRoles { get; set; } = [];
}
public class Permission : EntityBase
{
public required string Name { get; set; }
public string? Description { get; set; }
public ICollection<Role> Roles { get; set; } = [];
public ICollection<RolePermission> RolePermissions { get; set; } = [];
}
public class RolePermission : EntityBase
{
public required Guid RoleUuid { get; set; }
public Role? Role { get; set; }
public required Guid PermissionUuid { get; set; }
public Permission? Permission { get; set; }
}
public async Task<Role?> GetActiveWithPermissionByIdAsync(Guid uuid)
{
return await _context.Roles
.AsNoTracking()
.Include(r => r.Permissions.Where(p => p.DeletedAt == null))
.FirstOrDefaultAsync(r => r.Uuid == uuid && r.DeletedAt == null);
}