liveBook · Manning

Entity Framework Core

As explained in ASP.NET Core in Action, 3e

Entity Framework Core is a cross-platform .NET object-relational mapper for working with relational and some nonrelational databases through .NET types and queries.

Entity Framework Core (EF Core) is a cross-platform .NET library for database access. It is an object-relational mapper (ORM): it maps entity classes to tables, properties to columns, and objects to rows. You work with .NET objects while EF Core handles much of the connection, SQL generation, and result mapping. section 12.1.1 section 12.6 section 12.1.2

Start by choosing a database provider, such as SQLite, PostgreSQL, or SQL Server. Install its NuGet package and the EF Core design package, then define entity classes and a DbContext. Register the DbContext with ASP.NET Core dependency injection using a provider and connection string. EF Core builds its model from the entities and DbSet properties; its conventions provide a starting point that you can customize. section 12.2 section 12.2.1 section 12.6

A typical path from model to stored data is:

  • Create and apply a migration after defining or changing entities. Migrations record schema changes so an existing database can be updated rather than replaced. section 12.3
  • Use a LINQ query to describe data to retrieve; EF Core generates SQL and maps the results to objects. section 12.1.2
  • Keep database operations in a service layer rather than putting them directly in minimal API endpoint handlers. section 12.2

Updating an existing entity

In a recipe API, the service loads a recipe by ID with DbSet.FindAsync, checks that it exists, changes its properties, and calls DbContext.SaveChangesAsync. EF Core tracks changes and generates the database update. FindAsync can return an already-tracked entity without another database call. section 12.4.4 section 12.1.2

The abstraction saves work, but it does not remove the need to understand database behavior. Hidden query patterns can produce an N+1 problem, and applying migrations can cause data loss. Before production, consider column sizes, validation, concurrent updates, transient failures, and whether synchronous or asynchronous calls suit the application. EF Core is distinct from Entity Framework 6.x; moving between them is not a simple upgrade. section 12.1.2 section 12.3 section 12.5 section 12.1.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Works through provider setup, modeling, migrations, and entity updates, but defers relationship updates and deeper production guidance.
Examples
Uses a minimal recipe API with SQLite packages and a service method that loads, changes, and saves an entity.
What it assumes
Starts from a simple minimal API and introduces EF Core installation and configuration rather than application creation.
What you can do after
Apply it via EF Core to connect an ASP.NET Core app to a database, manage schema changes, and query or save entities.
How it connects
Connects object-relational mapping with LINQ, DbContext, dependency injection, database providers, and migrations.

Also explained in

Compare how 4 books explain Entity Framework Core →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.