liveBook · Manning

Chapter 12: Saving data with Entity Framework Core

What you will learn in chapter 12 of ASP.NET Core in Action, 3e

This chapter introduction frames EF Core as a .NET library for reducing database-access complexity and previews configuring it, evolving database schemas, and performing CRUD in an ASP.NET Core app.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction frames EF Core as a .NET library for reducing database-access complexity and previews configuring it, evolving database schemas, and performing CRUD in an ASP.NET Core app.

Key terms: Entity Framework Core, EF Core, data model, migrations, CRUD, database, connections, data store, database access code, Entity Framework 6.x

12.1 Introducing Entity Framework Core

Introduces Entity Framework Core as a higher-level way to bridge application code and databases, framing its purpose, tradeoffs, and mapping concepts.

Key terms: EF Core, ADO.NET, database access code, network connections, SQL statements, result data, database access, mapping, app code, database

12.1.1 What is EF Core?

This section defines EF Core as a cross-platform object-relational mapper that maps .NET objects to database data, chiefly for relational databases.

Key terms: Entity Framework Core, EF Core, object-relational mapper, ORM, classes, objects, relational database, SQL, provider model, SQL Server

12.1.2 Why use an object-relational mapper?

This section explains how an ORM such as EF Core simplifies database access, the tradeoffs it introduces, and when lower-level data access may be preferable.

Key terms: object-relational mapper, ORM, EF Core, LINQ, POCO, SQL, change tracking, properties, update statements, columns

12.1.3 When should you choose EF Core?

This section positions EF Core as the preferred ORM choice for new .NET applications while distinguishing it from the older EF 6.x framework.

Key terms: EF Core, Entity Framework 6.x, EF 6.x, ORM, object-relational mapper, ASP.NET Core Identity

12.1.4 Mapping a database to your application code

This section introduces the recipe API’s EF Core data model and explains how entities, DbContext, DbSet properties, and relationships become a database model.

Key terms: entity, POCO, DbContext, database context, internal model, DbSet<T>, AppDbContext, DbSet<Recipe>, ICollection<Ingredient>, many-to-one relationship

12.2 Adding EF Core to an application

This section outlines how to set up EF Core in the recipe API, separating database access into a service layer and previewing the configuration and migration workflow.

Key terms: database provider, EF Core NuGet packages, DbContext, entities, data model, DI container, service layer, RecipeService, minimal API endpoint handlers, EF Core

12.2.1 Choosing a database provider and installing EF Core

This section explains how EF Core database providers let an application use a chosen database and which NuGet packages to install for runtime and design-time support.

Key terms: EF Core, provider model, high-level API, SQL statements, database provider, NuGet package, .csproj, PackageReference, Npgsql.EntityFrameworkCore.PostgreSQL, Microsoft.EntityFrameworkCore.SqlServer

12.2.2 Building a data model

This section defines a convention-based EF Core data model using POCO entity classes, relationships, and a custom DbContext.

Key terms: POCO classes, convention over configuration, entity, primary key, RecipeId, IngredientId, foreign key, many-to-one relationship, ICollection<Ingredient>, Ingredients

12.2.3 Registering a data context

This section registers an EF Core AppDbContext in ASP.NET Core's DI container and configures it to use a database provider and environment-specific connection string.

Key terms: AppDbContext, dependency injection container, WebApplicationBuilder, Program.cs, AddDbContext<T>, scoped service, DbContextOptionsBuilder, UseSqlite, Microsoft.EntityFrameworkCore.SQLite, connection string

12.3 Managing changes with migrations

This section introduces EF Core migrations as versioned C# records that generate and apply schema changes to keep a database aligned with an evolving data model.

Key terms: schema, data model, database schema, EF Core migrations, migration, command-line tools, applying migrations, rollback, data loss

12.3.1 Creating your first migration

This section shows how to create an initial EF Core migration and apply it to create and update a database schema.

Key terms: Package Manager Console, Microsoft.EntityFrameworkCore.Tools, dotnet-ef, dotnet ef, AppDbContext, migration, Migrations folder, migration file, Migration designer.cs file, AppDbContextModelSnapshot.cs

12.3.2 Adding a second migration

This section shows how to evolve an EF Core data model by creating and applying a second migration, while explaining how migrations track database schema versions.

Key terms: EF Core migrations, dotnet ef migrations add, dotnet ef database update, Recipe, IsVegetarian, IsVegan, migration file, .designer.cs, AppDbContextModelSnapshot.cs, __EFMigrationsHistory

12.4 Querying data from and saving data to the database

This section introduces RecipeService as the place to implement recipe database operations while keeping business logic separate from HTTP handlers or Razor Pages.

Key terms: RecipeService, business logic, recipes, database, endpoint handlers, Razor Pages, HTTP-centric, decouples, test, migrations

12.4.1 Creating a record

This section shows how a validated POST request is mapped to Recipe and Ingredient entities and saved through EF Core.

Key terms: AppDbContext, Recipe, Ingredient, Add(), SaveChangesAsync(), tracked entities, CreateRecipeCommand, overposting, binding model, RecipeId

12.4.2 Loading a list of records

This section shows how to query EF Core for a filtered, projected list of recipe summaries and explains when LINQ work runs in SQL or on the client.

Key terms: RecipeSummaryViewModel, Recipe entity, Select(), projection, DbSet<Recipe>, IQueryable, LINQ, ToListAsync(), ToArrayAsync(), SingleAsync()

12.4.3 Loading a single record

This section shows how to query EF Core for one recipe by ID, project it and its related ingredients into a detail view model, and execute the query asynchronously.

Key terms: Where(), Select, SingleOrDefaultAsync(), RecipeId, RecipeDetailViewModel, Where clause, Ingredients, ORM, EF Core

12.4.4 Updating a model with changes

This section shows how to update an EF Core entity through read-modify-save, explains EF Core change tracking, and recommends soft deletion for recipes when appropriate.

Key terms: RecipeService, UpdateRecipe, UpdateRecipeCommand, Recipe, FindAsync(), DbSet, change tracking, DbContext, tracked entities, snapshot

12.5 Using EF Core in production applications

This section highlights EF Core and database concerns that must be addressed before deploying an application to production.

Key terms: EF Core, production-ready app, data-access code, scaffolding of columns, DataAnnotations validation attributes, Timestamp columns, concurrency, transient errors, connection resiliency, automatic migrations

12.6 Summary

This summary recaps how EF Core maps application entities to databases, is configured through a DbContext, and handles schema changes and data operations.

Where this is used later

Concepts explained in this chapter

← Chapter 11: Documenting APIs with OpenAPI Chapter 13: Creating a website with Razor Pages →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.