Select Data Using Entity Framework Async Call

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.   
  8. namespace SelectData_EF_Async  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             var employee = GetEmployeeData();  
  15.             employee.Wait();  
  16.             Console.ReadKey();  
  17.         }  
  18.         public static async Task GetEmployeeData()  
  19.         {  
  20.             using (var db = new EmployeeDBEntities())  
  21.             {  
  22.                 var query = await (from b in db.Employees  
  23.                                    select b).ToListAsync();  
  24.                 foreach (var r in query)  
  25.                 {  
  26.                     Console.Write(r.FirstName + "\n");  
  27.                 }  
  28.             }  
  29.         }  
  30.     }  
  31. }  
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.



MVC Corporation
MVC Corporation is consulting and IT services based company.