parko3
[EF Core 8] Many-to-many relationship with payload
Add this to your DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>()
.HasMany(e => e.Tags)
.WithMany(e => e.Posts)
.UsingEntity<PostTag>(
l => l.HasOne(e => e.Tag).WithMany(e => PostTags).HasForeignKey(e => e.TagId),
l => l.HasOne(e => e.Post).WithMany(e => PostTags).HasForeignKey(e => e.PostId);
}
7 replies