ORM for C# (.NET): Overview, Comparison, and Examples
Object-Relational Mapping (ORM) is a technology that allows developers to work with databases through object-oriented code, abstracting away from SQL queries. In the .NET ecosystem, there are several popular ORM libraries, each with its own strengths and weaknesses. In this article, we will look at three main tools: Entity Framework Core, Dapper, and NHibernate, compare their performance, ease of use, and provide practical code examples in C#.
1. Entity Framework Core (EF Core)
EF Core is the official ORM from Microsoft, which is the successor to Entity Framework 6. It supports Code First, Database First, and Model First approaches, and also provides a powerful LINQ provider for building queries.
Advantages:
- High abstraction — minimal SQL code.
- Automatic migrations and database schema management.
- Support for multiple providers (SQL Server, PostgreSQL, SQLite, MySQL).
- Deep integration with ASP.NET Core.
Disadvantages:
- Reduced performance on complex queries.
- High memory consumption when loading related data.
Code Example:
// Modelpublic class Product{ public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; }}
// Contextpublic class AppDbContext : DbContext{ public DbSet<Product> Products { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlServer("YourConnectionString");}
// Usageusing var db = new AppDbContext();var cheapProducts = db.Products .Where(p => p.Price < 100) .OrderBy(p => p.Name) .ToList();2. Dapper
Dapper is a lightweight micro-ORM created by the Stack Overflow team. It extends IDbConnection with extension methods and works as close to ADO.NET as possible, providing high performance.
Advantages:
- Extremely fast — almost like pure ADO.NET.
- Minimal memory consumption.
- Full control over SQL queries.
- Suitable for high-load systems.
Disadvantages:
- No automatic database schema generation.
- Need to write SQL queries manually.
- No Change Tracking.
Code Example:
using (var connection = new SqlConnection("YourConnectionString")){ var products = connection.Query<Product>( "SELECT * FROM Products WHERE Price < @Price ORDER BY Name", new { Price = 100 }); // Multiple results using (var multi = connection.QueryMultiple("SELECT * FROM Products; SELECT * FROM Categories")) { var prods = multi.Read<Product>(); var cats = multi.Read<Category>(); }}3. NHibernate
NHibernate is a port of the popular Java framework Hibernate. It provides a full implementation of a JPA-like approach with powerful caching and support for complex inheritance hierarchies.
Advantages:
- Advanced caching system (first and second level).
- Support for Lazy Loading, HQL (Hibernate Query Language).
- Flexible mapping configuration via XML or Fluent API.
- Well suited for legacy projects.
Disadvantages:
- Complex configuration and steep learning curve.
- Slower than Dapper and EF Core on simple queries.
- Rarely updated compared to EF Core.
Code Example:
Also in library
Processing Markdown in TypeScript: Libraries, Parsers, and Practical Applications
Thymeleaf for Java: Complete Guide with Examples
Working with XML in PHP: DOMDocument, SimpleXML and XPath
Event Bus and Observer Pattern in Go (Golang): pub/sub Implementation