Skip to content

Commit

Permalink
Add more M2M configuration samples
Browse files Browse the repository at this point in the history
Fixes #2880
  • Loading branch information
AndriySvyryd authored Dec 15, 2020
1 parent 99f6e4f commit 80a2154
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion entity-framework/core/extensions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Tools & Extensions - EF Core
description: External tools and extensions for Entity Framework Core
author: ErikEJ
ms.date: 22/11/2020
ms.date: 11/22/2020
uid: core/extensions/index
---

Expand Down
12 changes: 11 additions & 1 deletion entity-framework/core/modeling/relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,14 @@ CREATE TABLE [PostTag] (
);
```

Internally, EF creates an entity type to represent the join table that will be referred to as the join entity type. There is no specific CLR type that can be used for this, so `Dictionary<string, object>` is used. More than one many-to-many relationships can exist in the model, therefore the join entity type must be given a unique name, in this case `PostTag`. The feature that allows this is called shared-type entity type.
Internally, EF creates an entity type to represent the join table that will be referred to as the join entity type. `Dictionary<string, object>` is used for it to handle any combination of foreign key properties, see [property bag entity types](shadow-properties.md#property-bag-entity-types) for more information. More than one many-to-many relationships can exist in the model, therefore the join entity type must be given a unique name, in this case `PostTag`. The feature that allows this is called shared-type entity type.

The many to many navigations are called skip navigations as they effectively skip over the join entity type. If you are employing bulk configuration all skip navigations can be obtained from `GetSkipNavigations`.

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relationships/ManyToManyShared.cs?name=Metadata)]

#### Join entity type configuration

It is common to apply configuration to the join entity type. This action can be accomplished via `UsingEntity`.

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relationships/ManyToManyShared.cs?name=SharedConfiguration)]
Expand All @@ -313,9 +315,17 @@ Additional data can be stored in the join entity type, but for this it's best to

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relationships/ManyToManyPayload.cs?name=ManyToManyPayload)]

#### Joining relationships configuration

EF uses two one-to-many relationships on the join entity type to represent the many-to-many relationship. You can configure these relationships in the `UsingEntity` arguments.

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relationships/ManyToManyShared.cs?name=Components)]

> [!NOTE]
> The ability to configure many-to-many relationships was introduced in EF Core 5.0, for previous version use the following approach.
#### Indirect many-to-many relationships

You can also represent a many-to-many relationship by just adding the join entity type and mapping two separate one-to-many relationships.

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Relationships/ManyToMany.cs?name=ManyToMany&highlight=16-19,21-24)]
Expand Down
20 changes: 20 additions & 0 deletions samples/core/Modeling/FluentAPI/Relationships/ManyToManyShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.WithMany(p => p.Posts)
.UsingEntity(j => j.HasData(new { PostsPostId = 1, TagsTagId = "ef" }));
#endregion

#region Components
modelBuilder.Entity<Post>()
.HasMany(p => p.Tags)
.WithMany(p => p.Posts)
.UsingEntity<Dictionary<string, object>>(
"PostTag",
j => j
.HasOne<Tag>()
.WithMany()
.HasForeignKey("TagId")
.HasConstraintName("FK_PostTag_Tags_TagId")
.OnDelete(DeleteBehavior.Cascade),
j => j
.HasOne<Post>()
.WithMany()
.HasForeignKey("PostId")
.HasConstraintName("FK_PostTag_Posts_PostId")
.OnDelete(DeleteBehavior.ClientCascade));
#endregion
}
}

Expand Down

0 comments on commit 80a2154

Please sign in to comment.