Learn ASP.NET Core MVC with MSSQL Using Dapper ORM (RESTful)10 min read


In this blog, we will learn ASP.NET Core MVC with MSSQL using dapper asynchronously instead Data access layer & testing the API using google ARC tool

What is Dapper?

Dapper is simple object mapper for .NET and it is virtually as fast as using a raw ADO.NET data reader. An ORM is an Object Relational Mapper, Which is responsible for mapping between database and programming language. For that reason it is called King of micro ORM

We are going to follow points below

  • Create Table & Store procedure
  • Create ASP.NET Core Web API Application
  • Installing Dapper ORM
  • Using Dapper Async method
  • Using ARC Tool for testing the API

Create table & Store Procedure

Open SQL Server management studio. Create a new database & create a new ContactMaster table.

Creating a store procedure for CURD operation

Create ASP.NET Core Web API Application

Open Visual Studio tool.

Create a new project & choose project temple

Select ASP.NET Core Application in this list then click “Next”

Configure your new project and click “Create”

You will select ASP.NET Core Web API template

Now project has been ready to use

Installing Dapper ORM

Right click on your project. Select Manage NuGet Packages. We need to add this packages so search Dapper. Once you installed in your project you can able to use you code.

Install over the package manager console

Dapper:

Install-Package Dapper -Version 1.50.5

Dapper Feature:

  • Dapper will expend your IDbConnection interface with normal & Async multiple methods
  • Execute and queries method can use parameters from multiple different ways
  • The result returned by queries method can be mapped to multiple types

Using Dapper Async method

Let’s creating a solution as Repository

Create a model class with ContactMaster table column

Create an Interface in Repository using Async keyword

Create data access layer class with implement interface

Add the SQL Server connection string from appsetting.json file

Creating a controller to injected the ContactMasterRepository

We also need to update the project startup.cs file to include the new Repository in the services layer.

services.AddSingleton<IContactMasterRepository, ContactMasterRepository>();

Using ARC Tool for testing the API

Open ARC tool & send the URl “https://localhost:44309/api/ContactMaster”

We can able to see the result over the debug

You can download this solution from GitHub URL

Calculation

In this article, we have learned ASP.NET Core MVC with MSSQL using dapper asynchronously instead Data access layer & testing the API using google ARC tool