C
C#10mo ago
SWEETPONY

Find doesn't return my collection

Shift contains a property: Qualifications I'm trying to add them to Shifts and it works! But when I get Shift using FindCoreAsync Qualifications in null
protected override async Task AddCoreAsync(Shift shift, EventHandlingContext context)
{
shift.Created = timeProvider.GetUtcNow().DateTime;
dbContext.Shifts.Add(shift);
dbContext.ShiftQualifications.AddRange(shift.Qualifications!);
await dbContext.SaveChangesAsync(context.CancellationToken).ConfigureAwait(false);
}

public override async Task<Shift?> FindCoreAsync(Shift shift, EventHandlingContext context)
{
var foundShift = await dbContext.Shifts
.AsNoTracking()
.FirstOrDefaultAsync(
s => s.ScheduledStart == shift.ScheduledStart
&& s.Type == shift.Type
&& s.ResourceId == shift.ResourceId,
context.CancellationToken)
.ConfigureAwait(false);

return foundShift;
}
protected override async Task AddCoreAsync(Shift shift, EventHandlingContext context)
{
shift.Created = timeProvider.GetUtcNow().DateTime;
dbContext.Shifts.Add(shift);
dbContext.ShiftQualifications.AddRange(shift.Qualifications!);
await dbContext.SaveChangesAsync(context.CancellationToken).ConfigureAwait(false);
}

public override async Task<Shift?> FindCoreAsync(Shift shift, EventHandlingContext context)
{
var foundShift = await dbContext.Shifts
.AsNoTracking()
.FirstOrDefaultAsync(
s => s.ScheduledStart == shift.ScheduledStart
&& s.Type == shift.Type
&& s.ResourceId == shift.ResourceId,
context.CancellationToken)
.ConfigureAwait(false);

return foundShift;
}
3 Replies
SWEETPONY
SWEETPONYOP10mo ago
AddCoreAsync works well, I can write dbContext.Shifts.ToList and Shift will contain Qualifications
Omnissiah
Omnissiah10mo ago
context.CancellationToken is indented 1 level too much also i wouldn't use == in a datetime comparison, i would probably use an epsilon/interval
cap5lut
cap5lut10mo ago
hard to give some help without the actual context. from the given information i would log the actual query with all parameters logged as well for both methods and compare while looking at the actual data and compare where there could be a mismatch. im also not that experienced with ef core, but what everyone and every (not too shabby) tutorial told me is, that u use one dbcontext instance per interaction, u might do that or not, its not obvious so i wanted to mention it just in case
Want results from more Discord servers?
Add your server