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 on 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 much 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.

Web Application

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

MVC Project Template

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.

using System.Data.Entity;

namespace AsyncMvcEmployee.Models
{
    public class Employee
    {
        public enum Gender
        {
            Male,
            Female
        }

        public int ID { get; set; }
        public string Name { get; set; }
        public Gender Sex { get; set; }
        public string Post { get; set; }
        public decimal Salary { get; set; }
        public string City { get; set; }
    }

    public class EmployeeDbContext : DbContext
    {
        public DbSet<Employee> Employees { get; set; }
    }
}

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

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

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

 MVC Scaffolding

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

Database Context class

As you see in the preceding screenshot, I've checked the use of the 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.

public async Task<ActionResult> Index()
{
    return View(await db.Employees.ToListAsync());
}

The Index() method is now an asynchronous method that tells the compiler to generate callbacks for parts of the method body and automatically create a Task<ActionResult> object that is returned.

  1. You can also check out that the method return type is Task<ActionResult>, in which Task<T> shows the current work with the result of the same type (T).
  2. The await keyword calls the service. In the background, the method is split into two parts. The first part ends with the operations that are 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.

Employees Controller

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