18.1. Types of Relationships

Just as database tables can relate to each other, so can classes and objects. In fact, ORM translates relationships between objects into relationships between database rows.

This chapter introduces the tools needed to create meaningful relationships using ORM. Let’s consider the different types of relationships at a conceptual level. In later sections, we will learn how to implement these relationships using EntityFrameworkCore.

For the examples below, we use four classes:

  • Event - A class representing a coding event.
  • EventCategory - A class representing categories of coding events.
  • EventDetails - A class that encapsulates details about a single event, such as description, contact email, location, and so on.
  • Tag - A piece of metadata labeling an event. You can think of these as topics that an event might include, such as C#, ASP.NET, or JavaScript. An event can cover many topics, so it can have many tags.

The first two of these are familiar to you from our CodingEvents app. The EventDetails and Tag classes are new.

18.1.1. One-to-One

The simplest type of relationship is the one-to-one relationship. This occurs when each instance of type A may be related to only one instance of type B, and vice versa.

Note

In the following examples, arrows point in the direction of the relationship. If A points to B, then you can say that A knows about B.

While relationships in SQL are bidirectional, relationships in programming languages are unidirectional. In other words, if A knows about B, then B doesn’t necessarily know about A.

A single Event object on the left, with a double-sided arrow point to a single EventDetails object on the right

A one-to-one relationship between an Event object and an EventDetails object

An Event object should only have one collection of details, so it should only be related to one EventDetails object. Similarly, details about an event are specific to that event, so an EventDetails object should only be related to one Event object.

Examples

The following pairs of things generally have one-to-one relationships:

  1. People / driver’s licenses
  2. States / capital cities
  3. iPhones / serial numbers

It is not required that each instance of type A be related to an instance of type B. For example, a person may not have a driver’s license.

18.1.2. One-to-Many and Many-to-One

A one-to-many relationship occurs when each instance of type A may be related to more than one instance of type B, but each instance of B can only be related to a single instance of type A.

A single EventCategory object on the left, related to two Event objects on the right

A one-to-many relationship between EventCategory and Event objects

In this case, we say that A has a one-to-many relationship to B. A category can contain multiple items, therefore an EventCategory object may be related to multiple Event objects. But an event may only be in one category.

Examples

The following pairs of things generally have one-to-many relationships:

  1. Birth dates / people
  2. States / U.S. Representatives
  3. Model numbers / iPhones

When discussing the inverse relationship, we say that B has a many-to-one relationship to A.

Two Event objects on the left, related to a single EventCategory object on the right

A many-to-one relationship between Event and EventCategory objects

A many-to-one relationship operates in the opposite direction of a one-to-many relationship. The difference between the two is which side of the relationship knows about the objects on the other side. In C# terms, this will translate into a property on one class that references the other.

Examples

Many-to-one relationships are simply the opposite direction of one-to-many. Therefore, each of the following pairs has a many-to-one relationship.

  1. People / birth dates
  2. U.S. Representatives / states
  3. iPhones / model numbers

18.1.3. Many-to-Many

Many-to-many relationships occur when each instance of type A can be related to multiple instances of type B, and vice versa.

Three Event objects on the left, with various relationships to three Tag objects on the right

A many-to-many relationship between Event and Tag objects

An event can have multiple tags, and a tag may be associated with multiple events. Thus, we have a many-to-many relationship.

Examples

The following pairs of things generally have many-to-many relationships:

  1. Books / authors
  2. Recipes / ingredients
  3. Actors / movies

18.1.4. Check Your Understanding

Question

Match the following pairs with the appropriate relationship type:

  1. car / manufacturer
  2. car / title
  3. car / driver
  4. car / tire

Question

True/False: Suppose two C# classes, A and B, are in a one-to-many relationship. Then class A must contain a property for instances of B and B must have a property for instances of A.

  1. True
  2. False