This is the method inside the repository:
public async Task<IEnumerable<T>> FindAll()
{
IQueryable<T> query = this.Context.Set<T>();
return await query.Select(e => e).ToListAsync();
}
public class Program
{
static void Main(string[] args)
{
DB dB = new DB();
IUnitOfWork unitOfWork = new UnitOfWork(dB);
Task<IEnumerable<User>> users = unitOfWork.UserRepository.FindAll();
users.Wait();
Console.WriteLine(users);
}
}
When I run my code I am getting:
System.Runtime.CompilerServices.AsyncTaskMethodBuilder'1+AsyncStateMachineBox'1[System.Collections.Generic.IEnumerable'1[PasteBook.WebApi.Models.User],PasteBook.WebApi.Repositories.GenericRepository'1+d__5[PasteBook.WebApi.Models.User]]
The simplest option is just to use an async entry point:
public class Program
{
// async Main method, with a Task return type
static async Task Main(string[] args)
{
DB dB = new DB();
IUnitOfWork unitOfWork = new UnitOfWork(dB);
// It's fine to await within the async method
var users = await unitOfWork.UserRepository.FindAll();
foreach (var user in users)
{
Console.WriteLine(user);
}
}
}
(Note that this answer doesn't try to address whether you should be using this particular database access pattern - it's focused on "how to use await from a console app.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With