Closed Angelinsky7 closed 8 months ago
@Angelinsky7 The navigations to and from the join entity need to be used in this call:
modelBuilder.Entity<Post>()
.HasMany(e => e.Tags)
.WithMany(e => e.Posts)
.UsingEntity<PostTag>(
l => l.HasOne<Tag>().WithMany().HasForeignKey(e => e.TagId),
r => r.HasOne<Post>().WithMany().HasForeignKey(e => e.PostId));
For example:
modelBuilder.Entity<Post>()
.HasMany(e => e.Tags)
.WithMany(e => e.Posts)
.UsingEntity<PostTag>(
l => l.HasOne<Tag>(e => e.Tag).WithMany(e => e.PostTags).HasForeignKey(e => e.TagId),
r => r.HasOne<Post>(e => e.Post).WithMany(e => e.PostTags).HasForeignKey(e => e.PostId));
@ajcvickers oh ! wahou ! Thank you so much ! I'm sorry for the noise (i should have known). Where can i ask to make the change in the documentation ? (for the next one)
@Angelinsky7 The example is already in the docs: https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many#many-to-many-with-navigations-and-changed-foreign-keys
I'm trying to follow the microsoft doc for configuring many to many relationships : https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many
The creation of the table is not correct if i specify a ManyToMany configuration with
UsingEntity
and left + right configuration...I hope i didn't miss anything... Thanks !
Include your code
Working example :
create this :
NOT Working example:
create this :
with this kind of error :
Introducing FOREIGN KEY constraint 'FK_PostTag_Posts_PostId1' on table 'PostTag' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
And if we try to overcome the cascade delete...
create this :
Include example code
TestEfCoreManyToManyBug.zip
Include provider and version information
EF Core version: 7.0.15 Database provider: Microsoft.EntityFrameworkCore.SqlServer: 7.0.15 Target framework: NET 7.0 Operating system: Window 11 IDE: Visual Studio 2022 17.4