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 the synchronous programming to perform the basic CRUD (Create, Read, Update and Delete) functionality in ASP.NET MVC 5 Web Applications. We'll now use the 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 the 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 the further use. So, asynchronous programming enables the server resources to be used more efficiently.
The .NET Framework 4.5 makes the 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.
Step 3: Select the MVC Project Template from the One ASP.NET Wizard.
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;I
nitial Catalog=Employee;Integrated Security=True"
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:
Step 4: Select the Model class, Database Context class and select the check box of Use async controllers as in the following:
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:
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 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:
Summary
This article described the use of asynchronous programming with CRUD functionality with asynchronous programming in MVC 5. Thanks for reading the article.