Important: Dapper does not replace SQL — it replaces ADO.NET boilerplate code.
2. Install Dapper
Using NuGet Package Manager:
Install-Package Dapper
Or via .NET CLI:
dotnet add package Dapper
3. Create Database Connection (Core Concept)
Dapper works on top of IDbConnection.
SQL Server Example
using System.Data;
using System.Data.SqlClient;
public class DbConnectionFactory
{
private readonly string _connectionString;
public DbConnectionFactory(string connectionString)
{
_connectionString = connectionString;
}
public IDbConnection CreateConnection()
{
return new SqlConnection(_connectionString);
}
}
4. Simple SELECT Query (EF vs Dapper)
EF6
var users = context.Users
.Where(x => x.IsActive)
.ToList();
Dapper
using Dapper;
string sql = "SELECT * FROM Users WHERE IsActive = 1";
using (var connection = _dbFactory.CreateConnection())
{
var users = connection.Query<User>(sql).ToList();
}
Faster
Full SQL control
5. Parameterized Queries (Very Important)
Never concatenate SQL strings.
string sql = "SELECT * FROM Users WHERE UserId = @UserId";
var user = connection.QueryFirstOrDefault<User>(
sql,
new { UserId = 10 }
);
Prevents SQL Injection
Clean & readable
6. INSERT / UPDATE / DELETE (No SaveChanges)
Insert
string sql = @"
INSERT INTO Users (Name, Email)
VALUES (@Name, @Email)";
connection.Execute(sql, new
{
Name = "Anna",
Email = "anna@email.com"
});
Update
string sql = @"
UPDATE Users
SET Email = @Email
WHERE UserId = @UserId";
connection.Execute(sql, new
{
Email = "new@email.com",
UserId = 5
});
Delete
string sql = "DELETE FROM Users WHERE UserId = @UserId";
connection.Execute(sql, new { UserId = 5 });
7. Stored Procedures with Dapper
var users = connection.Query<User>(
"GetActiveUsers",
commandType: CommandType.StoredProcedure
).ToList();
public class UserRepository
{
private readonly DbConnectionFactory _factory;
public UserRepository(DbConnectionFactory factory)
{
_factory = factory;
}
public IEnumerable<User> GetAll()
{
using var conn = _factory.CreateConnection();
return conn.Query<User>("SELECT * FROM Users");
}
}
Clean
Testable
Production-ready
10. Replacing EF Gradually (Best Practice)
You don’t need to remove EF fully.
Hybrid Approach (Very Common)
EF → INSERT / UPDATE / DELETE
Dapper → SELECT / Reports / Search
Example:
// EF for write
context.Users.Add(user);
context.SaveChanges();
// Dapper for read
var users = connection.Query<User>("SELECT * FROM Users");
11. When Dapper Is a Better Choice Than EF
Use Dapper when:
Performance is critical
Queries are complex
Reporting / dashboards
Large data reads
Stored procedures
Stick with EF when:
Heavy business logic
Rapid development
Complex relationships
12. One-Line Interview Answer
“Dapper replaces Entity Framework by using raw SQL with object mapping, giving better performance and full SQL control at the cost of manual change tracking.”
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
1. What Changes When You Move from EF to Dapper?
DbContextIDbConnectionSaveChanges()INSERT/UPDATE)2. Install Dapper
Using NuGet Package Manager:
Or via .NET CLI:
3. Create Database Connection (Core Concept)
Dapper works on top of
IDbConnection.SQL Server Example
4. Simple SELECT Query (EF vs Dapper)
EF6
Dapper
5. Parameterized Queries (Very Important)
Never concatenate SQL strings.
6. INSERT / UPDATE / DELETE (No SaveChanges)
Insert
Update
Delete
7. Stored Procedures with Dapper
Passing parameters:
8. Handling Transactions (Manual but Clear)
9. Repository Pattern with Dapper (Recommended)
10. Replacing EF Gradually (Best Practice)
You don’t need to remove EF fully.
Hybrid Approach (Very Common)
INSERT / UPDATE / DELETESELECT / Reports / SearchExample:
11. When Dapper Is a Better Choice Than EF
Use Dapper when:
Stick with EF when:
12. One-Line Interview Answer