Simple question: I have an ASP.NET Core app that queries a SQL Server database using EF Core.
The client calls a method on a wrapper class that calls the methods, which get the data from the database.
The get data methods are like this:
public List<Person> GetPersons(string name)
{
// some logic to decide which table to query, bad design, I know
var list = dbContext.Persons.ToList();
// some more internal logic on the "list" variable.
return list;
}
I want to make the ToList()
call asynchronous and use ToListAsync()
however I am having trouble grasping the idea of async/await in this context, because if I do:
public List<Person> GetPersons(string name)
{
// some logic to decide which table to query, bad design, I know
var list = await dbContext.Persons.ToListAsync();
// some more internal logic on the "list" variable.
return list;
}
in order to free the thread while it's waiting for the DB to return data (this can take a while) I would have to make the method async, meaning I'd have to change List<Person>
to Task<List<Person>>
, meaning I'd have to change the calling method to await it, as well, meaning it'd have to make that async, too.
At least, that's how the compiler suggests and how I thought it might work.
Baseline - is there a way to make the code await the getting of data, without having to make every single method up the chain async and await, as well?