C
C#7mo ago
Letieri

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.
c#
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);
}
c#
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);
}
10 Replies
Kao
Kao7mo ago
It's cs and not C# for the coloration highlights You also should show you repo methods
Letieri
LetieriOP7mo ago
I managed to implement soft deletes in the other repositories; they fill the deletedAt field in the RolePermissions class as expected. My issue lies solely in retrieving the Roles with permissions where the association has been deleted.
Kringe
Kringe7mo ago
What is DeletedAt a bool or object or something else?
Kao
Kao7mo ago
Likely a nullable DateTime
Kringe
Kringe7mo ago
a ofc thanks
Letieri
LetieriOP7mo ago
yep
Kao
Kao7mo ago
So why are you trying to get the roles that have a Deletedaat value here? Wait no Brain farted :KEK: I'm eepy What is the result you have with your current code
Letieri
LetieriOP7mo ago
:kekw: It returns the permissions where I deleted the relationship using soft delete.
Kao
Kao7mo ago
Oh but you are going through Permissions instead of RolePermissions I think you need to include RolePermissions.Where(blabla is not null) And then to use the ThenInclude(RP.Roles)
Letieri
LetieriOP7mo ago
word up I'll test it out Thank you, the problem has been solved, now I just need to solve some mapping problems
Want results from more Discord servers?
Add your server