public record struct ImposterDbContextAuthorization(int AccountUserId, bool HasPermissionCanAccessAnyAccount, bool HasPermissionSuperUser = false) : IDbContextAuthorization;
< /code>
public async Task Send(int clinicId)
{
// added await here
Task.Run(async () =>
{
await SendIt(clinicId, DbContext.GetAccountId());
});
return "";
}
public async Task SendIt(int clinicId, int accountId)
{
var scope = _serviceScopeFactory.CreateScope();
var dbOptions = scope.ServiceProvider.GetRequiredService();
var imposter = new ImposterDbContextAuthorization(accountId, true);
var dbContext = new MyDbContext(dbOptions, imposter);
var allRecords = await dbContext.Set()
.AsNoTracking()
.Where(visit => visit.ClinicId == clinicId)
.GroupBy(visit => visit.PatientId)
.Select(group => group.Key).ToListAsync();
// !!! allRecords.Count is 0, even though "SELECT * FROM visit WHERE clinic_id = clinicId" returns 1 record in database
}
< /code>
I tried adding await to the Task.Run
Почему AllRecords возвращается с нулевыми записями, когда в базе данных есть записи? < /p> [code]public record struct ImposterDbContextAuthorization(int AccountUserId, bool HasPermissionCanAccessAnyAccount, bool HasPermissionSuperUser = false) : IDbContextAuthorization; < /code> public async Task Send(int clinicId) { // added await here Task.Run(async () => { await SendIt(clinicId, DbContext.GetAccountId()); }); return ""; } public async Task SendIt(int clinicId, int accountId) { var scope = _serviceScopeFactory.CreateScope(); var dbOptions = scope.ServiceProvider.GetRequiredService(); var imposter = new ImposterDbContextAuthorization(accountId, true); var dbContext = new MyDbContext(dbOptions, imposter);
var allRecords = await dbContext.Set() .AsNoTracking() .Where(visit => visit.ClinicId == clinicId) .GroupBy(visit => visit.PatientId) .Select(group => group.Key).ToListAsync(); // !!! allRecords.Count is 0, even though "SELECT * FROM visit WHERE clinic_id = clinicId" returns 1 record in database } < /code> I tried adding await to the Task.Run[/code], заставили записи return