CRUD Operations Using Asynchronous Programming in MVC 5

Introduction

In ASP.NET MVC 5 you have used the Scaffolding and worked with the Entity Framework. I also have used that scaffolding in the MVC 5 . To use scaffolding we can apply any approach of Entity Framework 6.

We've used synchronous programming to perform the basic CRUD (Create, Read, Update and Delete) functionality in ASP.NET MVC 5 Web Applications. We'll now use asynchronous programming for the CRUD functionality in the MVC 5 application.

Why Asynchronous Programming?

Generally there are very reserved worker processes available in the web server. If the load increases in the web server then it may be possible that all processes are in use. In that situation, the server cannot handle the new request until the processes are freed up. In synchronous programming, the worker processes may be tied up while they aren't actually doing any work because they are waiting for the I/O operations to complete. When we use asynchronous programming, when the I/O operations are not complete, the worker processes are freed up for further use. So, asynchronous programming enables the server resources to be used more efficiently.

The .NET Framework 4.5 makes asynchronous programming very easier.

Prerequisites

There are some prerequisites before starting with this article, given below:

  • Visual Studio 2013
  • MVC 5.1.1 (Latest)

However the preceding prerequisites are only applicable for performing the same as described in this article. You can perform this on MVC 5 applications also.

Getting Started

In this section, we'll create the MVC 5 application using the following sections:

  • Create ASP.NET MVC Application
  • Perform CRUD Functionality Asynchronously
  • Running MVC application

Create ASP.NET MVC Application

In this section, we'll create the application in Visual Studio 2013. Take a look at the  following procedure:

Step 1: Open Visual Studio 2013 and click on "New Project".

Step 2: Select the ASP.NET Web Application and enter the name.

Create Web Application in Visual Studio 2013

Step 3: Select the MVC Project Template from the One ASP.NET Wizard.

MVC Project Template in VS 2013

Visual Studio will automatically create the MVC application.

Perform CRUD Functionality Asynchronously

In this section we'll perform the CRUD functionality in this application. Use the following procedure.

Step 1: In the Solution Explorer, add a new class named "Employee" in the Models folder and update the code with the following code:

  1. using System.Data.Entity;  
  2. namespace AsyncMvcEmployee.Models  
  3. {  
  4.     public class Employee  
  5.     {  
  6.         public enum Gender  
  7.         {  
  8.             Male,  
  9.             Female  
  10.         }  
  11.         public int ID { getset; }  
  12.         public string Name{ getset; }  
  13.         public Gender Sex { getset; }  
  14.         public string Post { getset; }  
  15.         public decimal Salary { getset; }  
  16.         public string City { getset; }  
  17.     }  
  18.     public class EmployeeDbContext : DbContext  
  19.     {  
  20.         public DbSet<Employee> Employees { getset; }  
  21.     }  
  22. }   

Step 2: Save the EmployeeDbContext as a Database Context in the Connection Strings in the Web.Config file:

 

  1. <add name="EmployeeDbContext" connectionString="Data Source=(LocalDb)\v11.0;  
  2. AttachDbFilename=|DataDirectory|\Employee.mdf;I  
  3. nitial Catalog=Employee;Integrated Security=True"  
  4. providerName="System.Data.SqlClient" />  

 

Step 3: Now right-click on the Controllers folder to add a new Scaffolded item, select the MVC Scaffolding using Entity Framework with Views as in the following:

Scaffolding using Entity Framework in MVC 5

Step 4: Select the Model class, Database Context class and select the check box of Use async controllers as in the following:

Adding Controller using Asynchronous Programming

As you see in the preceding screenshot that I've checked the use async controller. After this the new scaffolded controller is generated and there were some changes applied when Entity Framework executed asynchronously. Those are given below:

  • Check out the following code:
    1. public async Task<ActionResult> Index()  
    2. {  
    3.     return View(await db.Employees.ToListAsync());  
    4. }
    The Index() method is now an asynchronous method that tells the compiler to generate callbacks for parts of the method body and create automatically a Task<ActionResult> object that is returned.
     
  • You can also check out that the method return type is Task<ActionResult> in which the Task<T> shows the current work with the result of the same type (T).
     
  • The await keyword calls the service. In the background, the method splits into two parts. The first part ends with the operations that is started asynchronously and the second part is put into a callback method that is called when the operation completes.

Running MVC Application

Now run the application, that works the same as previously. Run the Employees Controller as in the following:

Controller in MVC 5

Summary

This article described the use of asynchronous programming with CRUD functionality with asynchronous programming in MVC 5. Thanks for reading the article.


Similar Articles