C
C#3mo ago
SWEETPONY

✅ How to get all data without Include?

maybe stupid question but I want to ask it: I have two methods:
[UseProjection]
[Authorize(Roles = [Permissions.Rms.Task.Read])]
public Task<WorkingTaskWithFlights?> SingleWorkingTaskWithFlightAsync([Service(ServiceKind.Resolver)] ReadModelDbContext dbContext, IResolverContext context, string identity, [Service] NpgsqlHealthChecker npgsqlHealthChecker, CancellationToken cancellationToken)
=> HealthCheck(npgsqlHealthChecker, async () =>
{
var workingTask = await dbContext.WorkingTasks
.Include(workingTask => workingTask.RequiredQualifications)
.ThenInclude(workingTask => workingTask.Qualification)
.Include(workingTask => workingTask.StartLocation)
.Include(workingTask => workingTask.EndLocation)
.Include(workingTask => workingTask.WorkingShift)
.FirstOrDefaultAsync(workingTask => workingTask.Identity == identity, cancellationToken)
.ConfigureAwait(false);

if(workingTask == null)
return default;

var inboundIdentity = workingTask.CustomData?.InboundFlightLegIdentity;
var outboundIdentity = workingTask.CustomData?.OutboundFlightLegIdentity;

var flights = await dbContext.Flights
.Where(flight => flight.Identity == inboundIdentity || flight.Identity == outboundIdentity)
.ToListAsync(cancellationToken)
.ConfigureAwait(false);

return new WorkingTaskWithFlights { WorkingTask = workingTask, Flights = flights.AsReadOnly()};
});

[UseProjection]
[Authorize(Roles = [Permissions.Rms.Task.Read])]
public Task<WorkingTaskEntity?> SingleWorkingTaskAsync([Service(ServiceKind.Resolver)] ReadModelDbContext dbContext, IResolverContext context, string identity, [Service] NpgsqlHealthChecker npgsqlHealthChecker, CancellationToken cancellationToken)
=> GetByIdentityAsync(dbContext.WorkingTasks, identity, w => w.Project(context), npgsqlHealthChecker, cancellationToken);
[UseProjection]
[Authorize(Roles = [Permissions.Rms.Task.Read])]
public Task<WorkingTaskWithFlights?> SingleWorkingTaskWithFlightAsync([Service(ServiceKind.Resolver)] ReadModelDbContext dbContext, IResolverContext context, string identity, [Service] NpgsqlHealthChecker npgsqlHealthChecker, CancellationToken cancellationToken)
=> HealthCheck(npgsqlHealthChecker, async () =>
{
var workingTask = await dbContext.WorkingTasks
.Include(workingTask => workingTask.RequiredQualifications)
.ThenInclude(workingTask => workingTask.Qualification)
.Include(workingTask => workingTask.StartLocation)
.Include(workingTask => workingTask.EndLocation)
.Include(workingTask => workingTask.WorkingShift)
.FirstOrDefaultAsync(workingTask => workingTask.Identity == identity, cancellationToken)
.ConfigureAwait(false);

if(workingTask == null)
return default;

var inboundIdentity = workingTask.CustomData?.InboundFlightLegIdentity;
var outboundIdentity = workingTask.CustomData?.OutboundFlightLegIdentity;

var flights = await dbContext.Flights
.Where(flight => flight.Identity == inboundIdentity || flight.Identity == outboundIdentity)
.ToListAsync(cancellationToken)
.ConfigureAwait(false);

return new WorkingTaskWithFlights { WorkingTask = workingTask, Flights = flights.AsReadOnly()};
});

[UseProjection]
[Authorize(Roles = [Permissions.Rms.Task.Read])]
public Task<WorkingTaskEntity?> SingleWorkingTaskAsync([Service(ServiceKind.Resolver)] ReadModelDbContext dbContext, IResolverContext context, string identity, [Service] NpgsqlHealthChecker npgsqlHealthChecker, CancellationToken cancellationToken)
=> GetByIdentityAsync(dbContext.WorkingTasks, identity, w => w.Project(context), npgsqlHealthChecker, cancellationToken);
3 Replies
SWEETPONY
SWEETPONY3mo ago
the problem is second method doesn't need include to get all data but when I try to use Project in first method it doesn't work RequiredQualifications null etc hate chockolate is strange
Jimmacle
Jimmacle3mo ago
to load related data in EF you either need to 1. use .Include 2. reference the navigation property in your projection 3. configure the entity to auto-include (you probably don't want this)
SWEETPONY
SWEETPONY3mo ago
okay thanks