---
title: "How to use dapper instead of EF"  
description: "How to use dapper instead of EF"  
author: "Ravi Vishwakarma"  
published: 2026-01-21  
updated: 2026-01-22  
canonical: https://www.mindstick.com/forum/162021/how-to-use-dapper-instead-of-ef  
category: "asp.net"  
tags: [".net", "asp.net"]  
reading_time: 3 minutes  

---

# How to use dapper instead of EF

**How to use dapper [instead of](https://www.mindstick.com/articles/330/triggers-in-sql-server) EF, [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) with example?**

## Replies

### Reply by Anubhav Sharma

## 1. What Changes When You Move from EF to Dapper?

| EF6 | Dapper |
| --- | --- |
| `DbContext` | `IDbConnection` |
| LINQ queries | Raw SQL |
| Change tracking | Manual control |
| `SaveChanges()` | Explicit SQL (`INSERT/UPDATE`) |
| High abstraction | Low abstraction |

> **Important:**\
> Dapper does **not replace SQL** — it replaces **ADO.NET boilerplate code**.

## 2. Install Dapper

Using **NuGet Package Manager**:

```plaintext
Install-Package Dapper
```

Or via .NET CLI:

```plaintext
dotnet add package Dapper
```

## 3. Create Database Connection (Core Concept)

Dapper works on top of `IDbConnection`.

### SQL Server Example

```cs
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

```cs
var users = context.Users
                   .Where(x => x.IsActive)
                   .ToList();
```

### Dapper

```cs
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.

```plaintext
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

```cs
string sql = @"
INSERT INTO Users (Name, Email)
VALUES (@Name, @Email)";

connection.Execute(sql, new
{
    Name = "Anna",
    Email = "anna@email.com"
});
```

### Update

```cs
string sql = @"
UPDATE Users
SET Email = @Email
WHERE UserId = @UserId";

connection.Execute(sql, new
{
    Email = "new@email.com",
    UserId = 5
});
```

### Delete

```cs
string sql = "DELETE FROM Users WHERE UserId = @UserId";

connection.Execute(sql, new { UserId = 5 });
```

## 7. Stored Procedures with Dapper

```cs
var users = connection.Query<User>(
    "GetActiveUsers",
    commandType: CommandType.StoredProcedure
).ToList();
```

Passing parameters:

```cs
connection.Query<User>(
    "GetUserById",
    new { UserId = 10 },
    commandType: CommandType.StoredProcedure
);
```

## 8. Handling Transactions (Manual but Clear)

```cs
using (var connection = _dbFactory.CreateConnection())
{
    connection.Open();
    using (var transaction = connection.BeginTransaction())
    {
        try
        {
            connection.Execute(sql1, param1, transaction);
            connection.Execute(sql2, param2, transaction);

            transaction.Commit();
        }
        catch
        {
            transaction.Rollback();
            throw;
        }
    }
}
```

## 9. Repository Pattern with Dapper (Recommended)

```cs
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");
    }
}
```

1. Clean
2. Testable
3. 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:

```plaintext
// 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.”

##


---

Original Source: https://www.mindstick.com/forum/162021/how-to-use-dapper-instead-of-ef

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
