Introduction
First of all, let's have a clear look at Dapper and how it will be useful in our Core API. I think most of us know what Dapper is, but this article is for those who don't know about Dapper.
Dapper
Dapper is a simple Object Mapper and is nothing but Object-relational mapping (ORM) and is responsible for mapping between database and programming language and also it owns the title of King of Micro ORM in terms of speed. It is virtually as fast as using a raw ADO.NET data reader and also Entity Framework.
How Does Dapper Work?
- Creates an IDbConnection Object.
- Write a query to perform CRUD Operations
- Passes a Query as Parameter in the Execute Method.
Performance
Dapper is the Second Fastest ORM when compared with all Object-relational mappings.

Step 1
Create an ASP.NET Core project

Click on Next Button.
Step 2
Add a Project Name and Solution name to save the project to whichever location you want.

Click on Create Button.
Step 3
Choose the Appropriate version of API

Click on Create Button a sample project with basic setup will be created. Now let's dive into our project.
Create an empty API Controller with any name (Home)

Now Create the Services folder and add one Interface(IDapper.cs) and one Class(Dapperr.cs) to it.

Now add the ASP.NET Core Libraries to set up the database and also Dapper library into our project from the Nuget Package Manager.

Add the below code in IDapper.cs interface to where to perform the Crud Operations in our project.
- using Dapper;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Common;
- using System.Linq;
- using System.Threading.Tasks;
- namespace Dapper_ORM.Services
- {
- public interface IDapper : IDisposable
- {
- DbConnection GetDbconnection();
- T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);
- List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);
- int Execute(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);
- T Insert<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);
- T Update<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);
- }
- }
Add the code in Dapperr.cs File where the actual method implementation takes place in it for each and every method which we already declared in Interface
- using Dapper;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Common;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Threading.Tasks;
- namespace Dapper_ORM.Services
- {
- public class Dapperr : IDapper
- {
- private readonly IConfiguration _config;
- private string Connectionstring = "DefaultConnection";
- public Dapperr(IConfiguration config)
- {
- _config = config;
- }
- public void Dispose()
- {
- }
- public int Execute(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)
- {
- throw new NotImplementedException();
- }
- public T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.Text)
- {
- using IDbConnection db = new SqlConnection(_config.GetConnectionString(Connectionstring));
- return db.Query<T>(sp, parms, commandType: commandType).FirstOrDefault();
- }
- public List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)
- {
- using IDbConnection db = new SqlConnection(_config.GetConnectionString(Connectionstring));
- return db.Query<T>(sp, parms, commandType: commandType).ToList();
- }
- public DbConnection GetDbconnection()
- {
- return new SqlConnection(_config.GetConnectionString(Connectionstring));
- }
- public T Insert<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)
- {
- T result;
- using IDbConnection db = new SqlConnection(_config.GetConnectionString(Connectionstring));
- try
- {
- if (db.State == ConnectionState.Closed)
- db.Open();
- using var tran = db.BeginTransaction();
- try
- {
- result = db.Query<T>(sp, parms, commandType: commandType, transaction: tran).FirstOrDefault();
- tran.Commit();
- }
- catch (Exception ex)
- {
- tran.Rollback();
- throw ex;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (db.State == ConnectionState.Open)
- db.Close();
- }
- return result;
- }
- public T Update<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)
- {
- T result;
- using IDbConnection db = new SqlConnection(_config.GetConnectionString(Connectionstring));
- try
- {
- if (db.State == ConnectionState.Closed)
- db.Open();
- using var tran = db.BeginTransaction();
- try
- {
- result = db.Query<T>(sp, parms, commandType: commandType, transaction: tran).FirstOrDefault();
- tran.Commit();
- }
- catch (Exception ex)
- {
- tran.Rollback();
- throw ex;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (db.State == ConnectionState.Open)
- db.Close();
- }
- return result;
- }
- }
- }



Aseman ZaPosted Sep 18, 2022, 7:15 AM
Why you use dapper and EF both of them?why you need EF?
Alpesh ViradiyaPosted Feb 27, 2022, 3:59 AM
Hello Jay, thank you for the article. I am fresher. I need the same article in the MVC web application.
Shenbagapandiyan PPosted May 28, 2021, 7:14 AM
Jay Krishna Reddy I Couldn't understand the need for AppContext. Please explain why we have registered here?
fausto luisPosted Mar 20, 2021, 5:41 PM
If I'm wrong, correct me, please. Why didn't you use a generic asynchronous repository? Why Task.FromResult, it would be enough to return await in the controller's methods. By the way, why the use of entity framework? To communicate with the database? Seriously? Another thing, why "finally" to close the connection? Isn't the "using (...) instruction supposed to have that effect? Other than that, I consider the article well written and educational.
Hamid KhanPosted Mar 14, 2021, 12:29 PM
How we can add SP in Dapper
Hamid KhanPosted Mar 14, 2021, 12:29 PM
Nice explanation
Rakesh PathakPosted Feb 27, 2021, 3:54 PM
Return await dbContext.ExecuteAsync<Response>("highlights_sp_update", param: parameters, commandTimeout: 180, commandType: CommandType.StoredProcedure); I am getting error "Error CS1061 'IDbConnection' does not contain a definition for 'ExecuteAsync' and no accessible extension method 'ExecuteAsync' accepting a first argument of type 'IDbConnection' could be found (are you missing a using directive or an assembly reference?) "
mikepowertech mikepowertechPosted Jan 26, 2021, 9:52 AM
Thank you for the project. At the same time there is error: localhost page . And I checked my connection string is OK. Could it be Stored Procedures issue? Like here [dbo].[SP_Add_Article]" ? Hope to get more information on how to proper run your project, thank you!
Steven RiegerPosted Dec 30, 2020, 9:32 PM
@Jay, thank you for the article. I pulled the repo from git and updated the connection string. When I run it, I get the localhost page can not be found. Any thoughts as to why?
Shivaji KakadPosted Nov 8, 2020, 10:03 PM
@Jay, This article is really informative. I have just one concern here that instead of Registering dapper in Scoped, shouldn't we do it as Singleton? if not then please share your thoughts.
Muhammad BilalPosted Sep 29, 2020, 8:39 AM
Unable to connect with DB Configuration returning null please help me regarding that
sreenivasa kPosted Sep 2, 2020, 7:08 AM
Thank you Jay Krishna Reddy.
Jay Krishna ReddyPosted Aug 4, 2020, 7:47 AM
Thanks @Amit Mohanty
Amit MohantyPosted Aug 4, 2020, 7:37 AM
Nice article.
SriPosted Aug 3, 2020, 11:28 AM
Keep up the good work.