I have the following 3 models:
class Owner
{
public Guid Id { get; set; }
public IList<AccessShare> AccessShares { get; set; } = new List<AccessShare>()
}
class AccessShare
{
public Guid OwnerId { get; set; }
public Guid AccessorId { get; set; }
}
class File
{
public Guid OwnerId { get; set; }
public Owner Owner { get; set; }
public string Filename { get; set; }
}
The purpose of the system is assign ownership over files to one user, and to only allow other users to see those entities when there is an AccessShare from Accessor to Owner.
I have the following code that I'm using to bring back a file:
Guid fileId = <code that gets the accessor id>
Guid accessorId = <code that gets the accessor id>
File? file = DBContext
.Set<File>()
.Where(e => e.Id == fileId)
.Where(e => e.Owner.Id == accessorId || e.Owner.AccessShares.Any(a => a.AccessorId == accessorId))
.Include(e => e.Owner)
.Include(e => e.Owner.AccessShares)
.FirstOrDefault();
The issue I'm getting is that if null is returned, I don't know it that's because there isn't a File entity with the given id, or if there isn't an access share that allows access.
If this was raw SQL I'd do a left join from Owners to AccessShares with the above condition, this would always give me back the file/owner if found, and then optionally any access shares that meet the criteria.
I can find examples of how to do it in SQL and in Linq, but I can't find any examples using the DbSet fluid style.