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

