Skip to the workspace
Pebellslearn by building
Object Relational Mapping  ›  Lesson

ORMs and Dapper — Rows In, Objects Out

Dapper 14 minAn ORM maps database rows to C# objects; a micro-ORM like Dapper maps the results of SQL you write, while a full ORM also writes the SQL and tracks changes.

aspnet core / ORMs and Dapper — Rows In, Objects Out

Explore each scene at your own pace.

01 · The rule

The rule

ORMs and Dapper — Rows In, Objects Out

A database returns rows; your C# code wants Book objects. Copying each column into each property by hand, for every query, is tedious and error-prone. Object-relational mappers (ORMs) do the copying. How much more they do is the real choice.

The rule

Micro-ORM: you write the SQL, it maps the rows. Full ORM: it writes both.

.NET's ORMs sit on a scale:

  • Dapper — a micro-ORM. You write every SQL statement; it runs it with parameters and maps the result columns to properties by name. Tiny and very fast.
  • RepoDB — a hybrid: Dapper-style raw SQL plus built-in helpers for common operations.
  • Entity Framework Core — Microsoft's full ORM. You write LINQ in C#; it generates SQL, tracks changes to objects and manages the schema with migrations.
  • NHibernate — an older, mature full ORM, found in many long-lived codebases.

Dapper adds extension methods to any DbConnection (it opens a closed one for you):

public record Book(int Id, string Title, string Author, int Year);

await using var connection = new SqlConnection(connectionString);

var book = await connection.QuerySingleOrDefaultAsync<Book>(
    "SELECT Id, Title, Author, Year FROM Books WHERE Id = @Id",
    new { Id = id });

var recent = await connection.QueryAsync<Book>(
    "SELECT Id, Title, Author, Year FROM Books WHERE Year >= @From",
    new { From = 2000 });

await connection.ExecuteAsync(
    "UPDATE Loans SET ReturnedAt = @Now WHERE Id = @LoanId",
    new { Now = DateTime.UtcNow, LoanId = 40 });

The anonymous object supplies parameters: @Id is filled from Id. They travel separately from the SQL text, so a value can never change the query. Building SQL by pasting input into a string invites SQL injection.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.