8 Configuring relationships
This chapter covers
- Configuring relationships using By Convention
- Configuring relationships using Data Annotations
- Configuring relationships using Fluent API
- Five other ways to map entities to database tables
Chapter 7 described how to configure scalar (nonrelational) properties. This chapter covers how to configure database relationships. I assume you’ve read at least the first part of chapter 7, because configuring relationships uses the same three approaches, By Convention, Data Annotations, and the Fluent API, to map the database relationships.
This chapter covers how EF Core finds and configures relationships between entity classes, with pointers on how to configure each type of relationship—one-to-one, one-to-many, and many-to-many—and examples of each. EF Core’s By Convention relationship rules can quickly configure many relationships, but you’ll also learn about all the Data Annotations and Fluent API configuration options, which allow you to precisely define the way you want a relationship to behave. You’ll also look at features that allow you to enhance your relationships with extra keys and alternative table-mapping approaches. Finally, you consider five ways to map your classes to the database.