ORM for C# (.NET): comparison and examples
In modern development on the .NET platform, working with databases rarely goes without using Object-Relational Mapping (ORM). ORM libraries allow developers to interact with relational databases through object-oriented code, eliminating the need to write complex SQL queries manually. In this article, we will compare three of the most popular ORMs for C#: Entity Framework Core, Dapper, and LINQ to SQL, and also provide practical usage examples.
1. Entity Framework Core (EF Core)
EF Core is a modern, cross-platform ORM from Microsoft that is the evolution of Entity Framework 6. It supports working with various DBMS (SQL Server, PostgreSQL, SQLite, MySQL, etc.) and provides powerful capabilities for working with data.
Key features:
- Code First / Database First — you can create a database based on code or vice versa.
- LINQ queries — queries are written in C#, and EF Core converts them to SQL.
- Migrations — automatic management of the database schema.
- Lazy Loading — automatic loading of related data.
Code example (Code First):
using Microsoft.EntityFrameworkCore;using System.Collections.Generic;
public class User{ public int Id { get; set; } public string Name { get; set; } public string Email { get; set; }}
public class AppDbContext : DbContext{ public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=.;Database=MyDb;Trusted_Connection=True;"); }}
// Usageusing (var db = new AppDbContext()){ // Adding a user var user = new User { Name = "Ivan", Email = "ivan@example.com" }; db.Users.Add(user); db.SaveChanges();
// Query via LINQ var users = db.Users.Where(u => u.Name.Contains("Ivan")).ToList();}Pros: High performance, rich functionality, migration support, active community. Cons: Larger memory footprint, complexity when configuring complex queries.
2. Dapper
Dapper is a lightweight micro-ORM created by the Stack Overflow team. It does not provide full-fledged mapping, but allows you to execute SQL queries directly, automatically mapping results to C# objects.
Key features:
- Maximum performance — Dapper is considered one of the fastest ORMs for .NET.
- Direct SQL — you write queries in pure SQL.
- Minimalism — no migrations, contexts, or complex configurations.
Code example:
using Dapper;using System.Data.SqlClient;using System.Linq;
public class User{ public int Id { get; set; } public string Name { get; set; } public string Email { get; set; }}
// Usagestring connectionString = "Server=.;Database=MyDb;Trusted_Connection=True;";using (var connection = new SqlConnection(connectionString)){ // Executing an SQL query var users = connection.Query<User>("SELECT * FROM Users WHERE Name = @Name", new { Name = "Ivan" }).ToList();
// Executing a stored procedure var result = connection.Query<User>("GetUsers", commandType: System.Data.CommandType.StoredProcedure);}Pros: High speed, simplicity, minimal resource consumption. Cons: No migrations, you have to write SQL manually, no automatic loading of related entities.
3. LINQ to SQL
Also in library
Recommendations