liveBook · Manning

Many-to-many relationship

As explained in .NET in Action, Second Edition

A many-to-many relationship allows multiple instances of each of two entity types to be associated with multiple instances of the other.

A many-to-many relationship lets multiple books have multiple tags, and multiple tags apply to multiple books. In EF Core, collection properties on both entities describe the relationship; EF Core creates a join table with a compound primary key made from both entity IDs and foreign keys to each table. section 5.2.3

Listing 5.10 Many-to-many relationship between Book and Tag
class Book {
  public int Id {get;set;}
  public List<Tag> Tags {get;}
}
class Tag
{
  public int Id {get;set;}
  public List<Book> Books {get;}
}

The Book and Tag collection properties show how to represent the relationship in C#. section 5.2.3

How .NET in Action, Second Edition covers it

How deep it goes
Explains collection properties and the generated join table, including its compound primary key and foreign keys.
Examples
Uses Books and Tags, with each Book and Tag exposing a collection of the other.
What you can do after
Apply it via Entity Framework Core by defining collections on both entities and letting EF Core create the join table.
How it connects
Connects entity classes to relational tables, primary keys, foreign keys, and Entity Framework Core.

Also explained in

Compare how 5 books explain many-to-many relationship →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.