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

Entity Framework Core — LINQ Becomes SQL

Entity Framework Core 15 minEF Core maps entity classes to tables through a DbContext, translates LINQ queries into SQL when they are executed, and saves object changes with SaveChanges.

aspnet core / Entity Framework Core — LINQ Becomes SQL

Explore each scene at your own pace.

01 · The rule

The rule

Entity Framework Core — LINQ Becomes SQL

With Dapper you wrote every SQL statement for ReadingRoom. Entity Framework Core lets you write queries in C# with LINQ — the compiler checks property names, and EF writes the SQL. The one idea to master is when that SQL actually runs.

The rule

LINQ builds the query; ToListAsync runs it; SaveChangesAsync writes.

Entities are plain classes. A DbContext is your session with the database, and each DbSet is a table:

public class Book
{
    public int Id { get; set; }
    public string Title { get; set; } = "";
    public string Author { get; set; } = "";
    public int Year { get; set; }
}

public class LibraryDb(DbContextOptions<LibraryDb> options) : DbContext(options)
{
    public DbSet<Book> Books => Set<Book>();
    public DbSet<Member> Members => Set<Member>();
    public DbSet<Loan> Loans => Set<Loan>();
}

Register it once; each request then gets its own instance by injection:

builder.Services.AddDbContext<LibraryDb>(o =>
    o.UseSqlServer(builder.Configuration.GetConnectionString("Library")));

app.MapGet("/books", async (LibraryDb db) =>
    await db.Books
        .Where(b => b.Year > 2000)
        .OrderBy(b => b.Title)
        .ToListAsync());

By convention, a property named Id is the primary key, and the class Book maps to a table named after its DbSet, Books. Providers exist for SQL Server, PostgreSQL (Npgsql), SQLite and more; you swap them with one Use... call.

Where and OrderBy on a DbSet only describe the query (an IQueryable). Nothing touches the database until you execute it with ToListAsync, FirstOrDefaultAsync, CountAsync or a loop. EF then sends one SQL statement:

SELECT b.Id, b.Author, b.Title, b.Year
FROM Books AS b
WHERE b.Year > 2000
ORDER BY b.Title

Writing is just as direct: db.Books.Add(book); await db.SaveChangesAsync(); sends the INSERT.

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