7 Configuring relationships

 

This chapter covers

  • Configuring relationships using By Convention
  • Configuring relationships using Data Annotations
  • Configuring relationships using Fluent API
  • Other ways to map entities to database tables

Chapter 6 described how to configure scalar, or nonrelational, properties. This chapter covers how to configure database relationships. I assume you’ve read at least the first part of chapter 6, because configuring relationships uses the same three approaches, By Convention and 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.

7.1 Defining some relationship terms

 
 
 

7.2 What navigational properties do you need?

 
 
 

7.3 Configuring relationships

 
 

7.4 Configuring relationships By Convention

 
 

7.4.1 What makes a class an entity class?

 
 

7.4.2 An example of an entity class with navigational properties

 
 
 

7.4.3 How EF Core finds foreign keys By Convention

 
 
 
 

7.4.4 Nullability of foreign keys—required or optional relationships

 
 
 
 

7.4.5 Foreign keys—what happens if you leave them out?

 
 

7.4.6 When does By Convention configuration not work?

 

7.5 Configuring relationships by using Data Annotations

 
 

7.5.1 The ForeignKey Data Annotation

 
 
 

7.5.2 The InverseProperty Data Annotation

 
 
 

7.6 Fluent API relationship configuration commands

 
 
 
 

7.6.1 Creating a one-to-one relationship

 
 
 
 

7.6.2 Creating a one-to-many relationship

 
 
 
 

7.6.3 Creating a many-to-many relationship

 

7.7 Additional methods available in Fluent API relationships

 
 
 

7.7.1 OnDelete—changing the delete action of a dependent entity

 
 
 

7.7.2 IsRequired—defining the nullability of the foreign key

 
 

7.7.3 HasPrincipalKey—using an alternate unique key

 
 

7.7.4 Less-used options in Fluent API relationships

 
 
 

7.8 Alternative ways of mapping entities to database tables

 
 

7.8.1 Owned types—adding a normal class into an entity class

 
 

7.8.2 Table per hierarchy—placing inherited classes into one table

 
 

7.8.3 Table splitting—mapping multiple entity classes to the same table

 
 
 

Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest