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











