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.
dotnet --version should print 11.0.100 or a later .NET 11 version.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:
