The case study: Dapper vs Entity Framework Core
Tags: aspdotnet, ef, dapper, dotnetcore
Published: 2023-04-11 18:54:56

**Understanding Dapper**

Dapper is an open-source object-relational mapping (ORM) library for .NET and .NET Core applications. Dapper helps in accessing the data from databases without writing several lines of code. Dapper allows you to execute SQL queries and map the results to .NET objects. Since, It uses raw query developer have full control over the SQL that is getting executed. Moreover, this is developed by StackOverflow developers as a micro-ORM. The real power of this is observed while performing operations on large datasets.


```
using(var connection = new SqlConnection(connectionString)) {
    connection.Open();
    var employees = connection.Query <Employee>("SELECT * FROM employees");
    return employees.ToList();
}
```

**Understanding EF**

EF Core is a full-featured ORM framework providing a high-level abstraction over the database. EF Core generates SQL statements automatically, which can be slower than manually written SQL in some cases. It is optimised for developer productivity rather than performance.
EF Core also supports many databases, including SQL Server, PostgreSQL, MySQL, and SQLite

```
using(var context = new MyDbContext()) {
    var employees = context.Employees.ToList();
    return employees;
}
```

**Comparisons** 


![ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jt7md72ount8zsfptuzd.png)

- Flexibility
Dapper is more flexible as it gives control to the execution of the sql query. Though EF gives a robust and structured way to access data which makes it easier for projects of large projects. 

- Performance
Since Dapper is a lightweight  ORM it requires lesser memory space and faster startup time. It is designed in such a way that work faster in contrast with EF and ADO. Dapper achieves this by using raw SQL queries and avoiding the overhead of generating SQL statements dynamically. Meanwhile, EF Core provides caching and other performance optimisations that can sometimes make it faster.

- Complexity
Since, Dapper is lightweight it has fewer features. While, EF is a higher level of abstraction making it a better choice for large projects. Though conceptually and considering developers perspective dapper is far easier and less tedious. 

**Database supports**

1. EF Core
   -PostgreSQL
   -MySQL
   -MS SQLSERVER

2. Dapper
   -PostgreSQL
   -MySQL
   -MS SQLSERVER
   -SQLite
   -Oracle
   -IBM DB2

**Architectural working**


![Working](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1e7sssxmzdputyqpxrkb.png)


**Conclusion**
> Dapper is a good choice for applications that require high performance, less tedious development efforts and efficiency. It is a better choice for projects which require more control over the SQL that is generated. Meanwhile, EF is a better choice for a developer or projects which requires working on multiple data sources. EF Core is also a good choice for developers who prefer to work with a more fully featured ORM framework that generates SQL statements automatically. EF Core is also a good choice for applications that require features like change tracking, lazy loading, and query translation.

## Connects

Check out my other blogs: 
[Travel/Geo Blogs](shishsingh.wordpress.com)
Subscribe to my channel: 
[Youtube Channel](youtube.com/@destinationhideout)
Instagram:
[Destination Hideout](https://www.instagram.com/destinationhideout/)

## References

- _[Dapper vs EF Core by Selim Yıldız](https://levelup.gitconnected.com/dapper-vs-ef-core-which-orm-framework-should-you-choose-for-your-net-application-54f2723b176a)_
 

- _[The Big fight by Salih Cantekin](https://salihcantekin.medium.com/the-big-fight-dapper-vs-entity-framework-detailed-benchmark-2345af933382)_

- _[Dapper vs EF Core by c-sharpcorner](https://www.c-sharpcorner.com/article/dapper-vs-entity-framework-core/)_

- _EF and Dapper comparison by Simon Bonello_