DEV Community

Cover image for The case study: Dapper vs Entity Framework Core
Shish Singh
Shish Singh

Posted on • Updated on

The case study: Dapper vs Entity Framework Core

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();
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Comparisons

ORM

  • 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

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
Subscribe to my channel:
Youtube Channel
Instagram:
Destination Hideout

References

Top comments (0)