Entity Framework Core Fundamentals: Building Data-Driven Applications with .NET 11

Entity Framework Core Fundamentals: Building Data-Driven Applications with .NET 11

13+

Build Retail, a small data-driven application, with EF Core 11, SQL Server, migrations, queries, tests, and diagnostics.

0 followers
27 chapters
Software & AI Development
2026
You're viewing a limited preview. Create a free account to read free books or get Premium to unlock the entire library.

About This Book

Entity Framework Core Fundamentals teaches you to build the data layer of a .NET application with Entity Framework Core (EF Core) 11 and SQL Server. You build one application, a small retail system called Retail, from the first chapter to the last. Each chapter starts where the previous one ended and adds one thing, and each one shows you what to type and what you should see. By the end you can map a domain to tables, change the schema safely, write queries that send the SQL you meant, protect concurrent edits and look at what EF Core sends to the database.

Who this book is for

You can write C# classes, LINQ queries and async methods, and you know basic SQL words such as table, row, key and join. You have not used an ORM before. You do not need to know dependency injection or ASP.NET Core: Chapter 2 introduces dependency injection and Chapter 10 introduces ASP.NET Core.

What you will build

Retail starts in Chapter 1 as a console program with one Product class. Chapter 3 adds categories, customers, orders and order lines, and creates the database. Later chapters add migrations, saving changes, queries and paging, relationships, concurrency protection with SQL Server rowversion, a read service, a web API with health checks, automated tests, faster queries, an owned address, a payment hierarchy, soft delete, audit columns, deployment scripts and diagnostics. In Chapter 10 the same project becomes an ASP.NET Core web app, and everything built before stays.

Versions

The examples target net11.0 with the .NET 11 SDK, version 11.0.100 or later. Every EF Core package and the dotnet-ef command-line tool use version 11.0.0, and the project pins those versions so your results match the book's. SQL Server is the database throughout. Other databases translate queries and enforce rules differently; the companion's Experiments/ProviderComparison folder shows one difference with SQLite.

.NET 11 is a Standard Term Support release. Microsoft supports odd-numbered releases such as .NET 11 for two years and even-numbered Long Term Support releases for three. Check the current Microsoft .NET support policy when you choose a version for a production application.

Set up your computer

The labs run on Windows, because they use SQL Server Express LocalDB, a small edition of SQL Server that starts on demand on your own computer.

1. Install the .NET 11 SDK. In a terminal, dotnet --version should print 11.0.100 or a later .NET 11 version.
2. Install Visual Studio 2026 with the ASP.NET and web development workload. This is the main Windows path for creating projects, editing code, managing NuGet packages, debugging and running tests. VS Code with C# Dev Kit is the alternative; use its terminal for the command-line steps.
3. Install LocalDB. Visual Studio 2026 installs it with its ASP.NET and web development workload. With VS Code, install SQL Server Express LocalDB from Microsoft's SQL Server downloads.
4. Check LocalDB. Run SqlLocalDB info in a terminal. The list should include MSSQLLocalDB, the LocalDB instance every chapter uses (Chapter 3 shows this check in Figure 3.2). If PowerShell says it does not recognize SqlLocalDB, LocalDB is not installed yet.

The book uses Visual Studio for the application work and its PowerShell terminal for the pinned EF migration tool. Chapter 1 creates the solution and Chapter 2 explains launch profiles. When a terminal equivalent appears, use this mapping:

Task: Build

Visual Studio: Build > Build Solution, Ctrl+Shift+B

Terminal alternative: dotnet build

Task: Run without debugging

Visual Studio: Ctrl+F5 with Retail selected

Terminal alternative: dotnet run

Task: Run under the debugger

Visual Studio: F5 after setting a breakpoint

Terminal alternative: Use the C# Dev Kit debugger in VS Code

Task: Run a named lab

Visual Studio: Set Command line arguments to --lab N, then Ctrl+F5

Terminal alternative: dotnet run -- --lab N

Task: Print SQL

Visual Studio: Add --ShowSql true to the application arguments

Terminal alternative: dotnet run -- --ShowSql true

Task: Run tests

Visual Studio: Test > Test Explorer, then Run All Tests

Terminal alternative: dotnet test Retail.Tests from the solution folder

Task: Create or apply a migration

Visual Studio: View > Terminal, in Retail

Terminal alternative: The same pinned dotnet ef commands

The -- after dotnet run belongs to the CLI, so leave it out of Visual Studio's application argument box. Stop a debug session with Shift+F5 before changing profiles or code.

Retail keeps its data in one LocalDB database, EfCoreBook_Retail. From Chapter 3, every lab except Chapter 4's (which applies migrations to the database as it is) drops and rebuilds that database before it runs, so every run starts from the same data and prints the same result. The database stays after the run so you can look inside it. The rebuild refuses servers other than LocalDB and names without the EfCoreBook_ prefix. That is a guard, not a backup: reserve those names for disposable book labs and never store valuable data in them. Chapter 2 shows where the connection string lives.

Nothing in the companion code contacts a mail, payment or cloud service. The web API in Chapters 10 and 11 listens only on your own computer (127.0.0.1) and is for learning. A public service also needs authentication, authorization, HTTPS, protected secrets and a separate migration process; Chapter 15 covers the migration part.

The companion code

Download the companion code free at unqbd.com/go/ef2-code and unzip it. Its folders look like this:

global.json

SDK version for every chapter

.config/dotnet-tools.json

the dotnet-ef tool (Chapter 4 on)

Chapter01/EfCoreBook.slnx

open this chapter in Visual Studio

Chapter01/Retail

the project at the end of Chapter 1

Chapter12/Retail

the project at the end of Chapter 12

Chapter12/Retail.Tests

its tests (Chapter 12 on)

Chapter16/Retail

the finished project

Activities/ChapterNN

Activity solution (RetailActivity)

Experiments

SQLite comparison, complex types

Each ChapterNN/Retail folder is the Retail project as it stands at the end of that chapter. Use the folders in two ways:

  • To check your work, compare your files with the folder of the chapter you finished.
  • To start a chapter without your own project, copy the previous chapter's folder inside the companion folder: for Chapter 7, copy Chapter06 and give the copy a name of your own. The global.json and tool manifest at the companion's root apply to every folder below it. Open the copy's EfCoreBook.slnx with File > Open > Project/Solution, select Retail as the startup project and build the solution. From Chapter 4 on, open View > Terminal in the copy's Retail folder and run dotnet tool restore once so the migration commands work. Keep the copy inside the extracted companion folder so its root global.json and tool manifest still apply.

Each chapter adds a lab: a short class in the project's Labs folder that runs what the chapter built and prints the result. In Chapters 2 to 9, dotnet run runs the newest lab and dotnet run -- --lab 5 runs Chapter 5's again; Chapter 2 shows how this works. From Chapter 10, dotnet run serves the web app and --lab runs one lab and stops.

Each chapter ends with an Activity. Keep a separate folder, Activities/ChapterNN, for each one, with a RetailActivity project copy and an EfCoreBook_ActivityNN database. Chapter 12 copies both Retail and Retail.Tests instead. Your main project and earlier exercises stay intact. The companion supplies an EfCoreBook.slnx in every Activities/ChapterNN folder for its finished solution.

If something goes wrong:

  • "Set ConnectionStrings:DefaultConnection." means the app did not start in the Development environment. Run dotnet run from the Retail folder; Chapter 2 explains why.
  • "Unable to retrieve project metadata" from a dotnet ef command means the project has not been restored yet. Run dotnet build, then the command again.
  • An error that names the Local Database Runtime means LocalDB is not installed or not started. Check SqlLocalDB info.

Enjoying this book?

Create a free account to leave comments, track your progress, and more!

Read and listen on the go with the unQbd app. Get the app
An unhandled error has occurred. Reload 🗙

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please reload the page.