Skip to content

Provide more many-to-many mapping examples, including with explicitly mapped join entities #3648

Closed

Description

@blogcraft

Now that relationship tables are gone with EF Core 6, there must be some docs that explain how to join many to many tables.

Bejore:

from x in xTable
join xy in xyTable on x.Id equals xy.xId
join y in yTable on xy.yId equals y.Id
....

Now:
???

xyTable is no longer mapped to explicit entity types, so how to do a join? and multiple joins with multiple many to many relationships?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Activity

  1. ajcvickers commented on Jan 3, 2022

    @ajcvickers
    Contributor

    @blogcraft If you want to use explicit joins, then map the join table to an entity type and proceed as before.

  2. blogcraft commented on Jan 3, 2022

    @blogcraft
    Author

    Thanks for the response! But I didn't understand. (Sorry 😅) Can I get a code example?

  3. ajcvickers commented on Jan 10, 2022

    @ajcvickers
    Contributor

    @blogcraft For example, with a many-to-many between posts and tags:

    public class Post
    {
        public int Id { get; set; }
        public List<Tag> Tags { get; } = new();
    }
    
    public class Tag
    {
        public int Id { get; set; }
        public List<Post> Posts { get; } = new();
    }

    Create an entity type for the join table:

    public class PostTag
    {
        public int PostId { get; set; }
        public int TagId { get; set; }
    }

    And map it:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .Entity<Post>()
            .HasMany(e => e.Tags)
            .WithMany(e => e.Posts)
            .UsingEntity<PostTag>();
    }

    Now use it in your queries as before:

    var query = from x in context.Posts
        join xy in context.Set<PostTag>() on x.Id equals xy.PostId
        join y in context.Tags on xy.TagId equals y.Id
        select x;
  4. changed the title [-]Examples in how to do many to many joins in EF Core 6[/-] [+]Provide more many-to-many mapping examples, including with explicitly mapped join entities[/+] on Jan 10, 2022
  5. blogcraft commented on Jan 12, 2022

    @blogcraft
    Author

    Thanks for the example, I'll give it a shot!

    Sadly upgrading to EF 6 is more of a hassle thanks to this 😕 Our model is crawling with many to many relationships and we do database first reverse engineer with EF Core Power Tools every other day. Is there any benefit of doing explicit joins?
    I'm really considering staying with EF 5.

  6. ErikEJ commented on Jan 12, 2022

    @ErikEJ
    Contributor

    @blogcraft not sure if it is relevant but EF Core Power Tools has an option to generate join Entities with EF Core 6.

  7. blogcraft commented on Jan 12, 2022

    @blogcraft
    Author

    @ErikEJ Thanks a lot! It was exactly what I needed.

  8. self-assigned this
    on Jan 13, 2022
  9. added this to the Backlog milestone on Jan 13, 2022
  10. added 8 commits that reference this issue on Feb 25, 2023
    e20432f
    671a66c
    94ff962
    f33e5ed
    c7b38aa
    93b4c5e
    1ba858c
    bb39d2d
  11. added a commit that references this issue on Mar 19, 2023
    9a1e6f2
  12. added 2 commits that reference this issue on Mar 28, 2023
    f5d7299
    dd7c736
  13. modified the milestones: Backlog, 8.0.0 on Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions