6

I'd like to perform a couple of SELECT stored procedure calls in parallel. I setup my context as follows:

public virtual DbSet<Task<GetCaseCaseContextModel>> CaseGetCaseContextData { get; set; }

My repo class then does the following:

public async Task<List<GetCaseNotesContextModel>> GetCaseNotes(string caseId)
{
    var notes = _context.CaseGetCaseContextData.FromSql("x_Fortellis_CaseGetCaseNotes @p0", caseId, caseId).ToListAsync();

    return notes;
}

But I get the following error:

Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<System.Threading.Tasks.Task<CaseManagement.Infrastructure.Database.Repo.Case.GetCase.GetCaseCaseContextModel>>>' to 'System.Collections.Generic.List<CaseManagement.Infrastructure.Database.Repo.Case.GetCase.GetCaseNotesContextModel>' CaseManagement.Infrastructure.Database C:\Dev\Bitbucket\webscv\Fortellis\CaseManagement\CaseManagement.Infrustructure.Database\Repo\Case\GetCase\GetCaseRepoHelper.cs 72 Active

When I change the context to:

public virtual Task<DbSet<GetCaseCaseContextModel>> CaseGetCaseContextData { get; set; }

I get the following error:

Severity Code Description Project File Line Suppression State Error CS1061 'Task<DbSet>' does not contain a definition for 'FromSql' and no accessible extension method 'FromSql' accepting a first argument of type 'Task<DbSet>' could be found (are you missing a using directive or an assembly reference?) CaseManagement.Infrastructure.Database C:\Dev\Bitbucket\webscv\Fortellis\CaseManagement\CaseManagement.Infrustructure.Database\Repo\Case\GetCase\GetCaseRepoHelper.cs 70 Active

I'm ultimately want to call them using:

GetCaseCaseContextModel caseData = new GetCaseCaseContextModel();
List<GetCaseNotesContextModel> notes = new List<GetCaseNotesContextModel>();

Parallel.Invoke(
    async () => caseData = await GetCaseData(caseId, dealerGroupId),
    async () => notes = await GetCaseNotes(caseId)
);

return new GetCaseContextModel()
{
    CaseData = caseData,
    Notes = notes
};

Thanks Alex

3
  • 1
    The context property should be public virtual DbSet<GetCaseCaseContextModel> CaseGetCaseContextData { get; set; } or public virtual DbQuery<GetCaseCaseContextModel> CaseGetCaseContextData { get; set; } , i.e. no Task inside. But you can't use EF context from multiple threads.
    – Ivan Stoev
    Mar 20, 2019 at 18:45
  • Thanks. That is what I thought, but could not find it in the documentation. Mar 21, 2019 at 15:30
  • you forgot the await keyword before _context
    – Elyas Esna
    Apr 2, 2019 at 18:58

1 Answer 1

8

You have to rewrite with ToListAsync:

public async Task<List<GetCaseNotesContextModel>> GetCaseNotes(string caseId)
{
    var notes = await _context.CaseGetCaseContextData.FromSql("x_Fortellis_CaseGetCaseNotes @p0", caseId, caseId)
                         .ToListAsync();
    return notes;
}

Put await

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.