liveBook · Manning

Entity

As explained in .NET in Action, Second Edition

A database entity is an application object or distinct unit of data whose state is represented and persisted in a database.

An entity is a C# application object that EF Core maps to a data source and persists as database data. An identity property such as Id distinguishes each instance. section 5.6 section 5.2.3

Model entity relationships through properties that EF Core can interpret:

  • Use an optional navigation property for a one-to-one association, such as Book.Rating and Rating.Book. section 5.2.3
  • Use a collection navigation property for one-to-many associations, such as Book.Ratings. section 5.2.3
  • Use collections on both entities for many-to-many associations, such as Book.Tags and Tag.Books. section 5.2.3
  • Let conventions connect a property such as Rating.BookId to Book.Id, or configure foreign keys explicitly when conventions do not fit. section 5.2.3

Persisting and loading related entities

Create a Book, add Rating objects to its Ratings collection, add the Book to a CatalogContext, and call SaveChanges. Query the book with Include(b => b.Ratings) when printing its related ratings. section 5.2.3

Listing 5.8 One-to-one relationship with Book as the parent and Rating as the child
class Book {
  public int Id {get;set;}
  public Rating? Rating {get;}
}
class Rating
{
  public int Id {get;set;}
  public int BookId {get;set;}
  public Book Book {get;set}
}

Inspect the focused C# model showing a parent Book entity, a dependent Rating entity, navigation properties, and a foreign-key property. section 5.2.3

How .NET in Action, Second Edition covers it

How deep it goes
Explains properties, relationships, conventions, generated tables, persistence, and eager loading, but defers detailed explicit configuration.
Examples
Uses Book, Rating, and Tag classes plus CatalogContext code that saves ratings and loads them with Include.
What you can do after
Apply it via EF Core by defining entity classes, modeling relationships, persisting objects, and loading related data.
How it connects
Connects entities with relational databases, primary keys, foreign keys, navigation properties, and one-to-one, one-to-many, and many-to-many relationships.

Also explained in

Compare how 12 books explain entity →

Related concepts

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