Dapper versus EF Core
Dapper versus EF Core
Dapper and EF Core both use relational providers, but at different levels. Dapper maps explicit SQL results; EF Core builds a model of entities and can translate LINQ into queries, track entity changes, and persist them. An entity is an object with a database identity; a DbContext manages a unit of work. Choose based on query requirements, maintainability, provider support and measured behavior, not a universal performance ranking.
Proposed baseline: .NET 10 / C# 14 (SDK 10.0.404 inherited). Private SQLite in-memory fixture, Dapper 2.1.100 or EF Core SQLite 10.0.15 candidate dependencies. Approve dependencies before restore; no external database needed. Outputs are reasoned, not executed.
Read a filtered projection with Dapper
Keep the selected columns and filter visible in SQL.
using System;using System.Linq;using Dapper;using Microsoft.Data.Sqlite;using var db = new SqliteConnection("Data Source=:memory:;Pooling=False");db.Open();db.Execute("CREATE TABLE Books (Id INTEGER PRIMARY KEY, Title TEXT NOT NULL, Stock INTEGER NOT NULL CHECK(Stock >= 0))");db.Execute("INSERT INTO Books VALUES (1, 'Atlas', 4), (2, 'Notebook', 1)");var titles = db.Query<string>("SELECT Title FROM Books WHERE Stock >= @Minimum ORDER BY Id", new { Minimum = 2 });Console.WriteLine(string.Join(",", titles));The query text specifies both the filter and projection. Dapper does not track the resulting strings.
Expected observation (reasoned, not executed):
AtlasRead the equivalent projection with EF Core
The model maps Book objects to the Books table. Use LINQ against a DbSet and materialize with ToList.
using System;using System.Linq;using Microsoft.Data.Sqlite;using Microsoft.EntityFrameworkCore;using var db = new SqliteConnection("Data Source=:memory:;Pooling=False");db.Open();var options = new DbContextOptionsBuilder<Catalog>().UseSqlite(db).Options;using var ctx = new Catalog(options);ctx.Database.EnsureCreated();ctx.Books.AddRange(new Book { Id = 1, Title = "Atlas", Stock = 4 }, new Book { Id = 2, Title = "Notebook", Stock = 1 });ctx.SaveChanges();ctx.ChangeTracker.Clear();var titles = ctx.Books.Where(b => b.Stock >= 2).OrderBy(b => b.Id).Select(b => b.Title).ToList();Console.WriteLine(string.Join(",", titles));public class Book{ public int Id { get; set; } public string Title { get; set; } = ""; public int Stock { get; set; }}public class Catalog(DbContextOptions<Catalog> options) : DbContext(options){ public DbSet<Book> Books => Set<Book>();}The SQLite provider translates this query to SQL; only the selected title values are materialized. Exact SQL is provider/version dependent.
Expected observation (reasoned, not executed):
AtlasObserve tracking versus no tracking
Modify an untracked entity first, then a tracked entity. Requery from the database after saving.
using System;using System.Linq;using Microsoft.Data.Sqlite;using Microsoft.EntityFrameworkCore;using var db = new SqliteConnection("Data Source=:memory:;Pooling=False");db.Open();var options = new DbContextOptionsBuilder<Catalog>().UseSqlite(db).Options;using var ctx = new Catalog(options);ctx.Database.EnsureCreated();ctx.Books.AddRange(new Book { Id = 1, Title = "Atlas", Stock = 4 }, new Book { Id = 2, Title = "Notebook", Stock = 1 });ctx.SaveChanges();ctx.ChangeTracker.Clear();var detached = ctx.Books.AsNoTracking().Single(b => b.Id == 1);detached.Stock = 9;ctx.SaveChanges();Console.WriteLine(ctx.Books.AsNoTracking().Single(b => b.Id == 1).Stock);var tracked = ctx.Books.Single(b => b.Id == 1);tracked.Stock = 3;ctx.SaveChanges();Console.WriteLine(ctx.Books.AsNoTracking().Single(b => b.Id == 1).Stock);public class Book{ public int Id { get; set; } public string Title { get; set; } = ""; public int Stock { get; set; }}public class Catalog(DbContextOptions<Catalog> options) : DbContext(options){ public DbSet<Book> Books => Set<Book>();}The detached change is not known to the change tracker. A tracked entity change is detected and persisted by SaveChanges.
Expected observation (reasoned, not executed):
43Common mistakes and edge cases
Do not use EnsureCreated as a production migration strategy. It is suitable here only for a new disposable database. Keep DbContext short-lived and do not run concurrent operations on one context. A SQLite in-memory fixture is not proof of SQL Server or PostgreSQL translation or isolation. A repository can expose an application-oriented operation (such as TryReserveBook) without leaking connection or context lifetime; do not blindly wrap every DbSet in generic CRUD.
Practice and recap
Choose an implementation for a reporting endpoint with hand-tuned SQL and for an order edit that changes related entities. Describe what correctness and performance tests you need.
Hint: Compare query control and change tracking.
Learning objective: Choose a data abstraction with justified trade-offs. See accompanying manual practice and knowledge check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dapper versus EF Core — knowledge check
Apply the lesson concepts to the following scenarios.
What does DbContext represent here?
Which Dapper responsibility stays with the developer?
Does changing AsNoTracking result automatically save it?
Why keep a SQLite memory connection open?
Which performance conclusion is justified by these examples?