Introduction

This article will show you how to select data using an Entity Framework async call.

Create a console application as in the following figure:


Figure 1: Create console application

Add Entity Framework to project as in Figures 2, 3 and 4.

Figure 2: Add Entity Framework

Figure 3: Choose Connection

Figure 4: Choose Database Object

Program.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.Entity;
  6. using System.Threading.Tasks;
  7. namespace SelectData_EF_Async
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. var employee = GetEmployeeData();
  14. employee.Wait();
  15. Console.ReadKey();
  16. }
  17. public static async Task GetEmployeeData()
  18. {
  19. using (var db = new EmployeeDBEntities())
  20. {
  21. var query = await (from b in db.Employees
  22. select b).ToListAsync();
  23. foreach (var r in query)
  24. {
  25. Console.Write(r.FirstName + "\n");
  26. }
  27. }
  28. }
  29. }
  30. }
The output of the application is as in Figure 5.

Figure 5: Output of the application

Summary

In this article we saw how to select data using an Entity Framework async call.