liveBook · Manning

Entity-relationship model

As explained in .NET in Action, Second Edition

An entity relationship is an association between entities or records that expresses how their data is connected.

An entity-relationship model represents how related entities connect in a relational application. In Entity Framework Core, navigation properties describe associations, while conventions infer foreign keys and relationship cardinality. section 5.2.3

Common relationship shapes include:

  • One-to-one: a principal entity may have one optional dependent, such as Book and Rating. section 5.2.3
  • One-to-many: a principal entity exposes a collection of dependents, such as Book and multiple Ratings. section 5.2.3
  • Many-to-many: both entities expose collections, such as Books and Tags. section 5.2.3
  • EF Core conventionally matches a property such as Rating.BookId with Book.Id; explicit foreign keys are available when conventions fail. section 5.2.3

Persist and query ratings

Create a Book, add Rating objects to its Ratings collection, save the context, and query the book with Include(b => b.Ratings) before printing each rating. section 5.2.3

Listing 5.9 One-to-many relationship with Book as the parent and Rating as the child
class Book {
  public int Id {get;set;}
  public List<Rating> Ratings
    {get;}
}
class Rating
{
  public int Id {get;set;}
  public int BookId {get;set;}
  public Book Book {get;}
}

Inspect the C# shape of a one-to-many relationship, including the dependent foreign key and both navigation properties. section 5.2.3

How .NET in Action, Second Edition covers it

How deep it goes
Explains cardinality, navigation properties, conventions, persistence, and loading, while deferring explicit database enforcement techniques.
Examples
Uses Book, Rating, and Tag classes plus EF Core save, query, Include, and console-printing examples.
What it assumes
Assumes C# classes and relational database familiarity while teaching relationship terminology and EF Core conventions.
What you can do after
Builds EF Core entities with one-to-one, one-to-many, or many-to-many associations and queries related data.
How it connects
Connects entities, records, navigation properties, foreign keys, primary keys, joins, and relational databases.

Also explained in

Compare how 2 books explain entity-relationship model →

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.