Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
blogcraft opened this issue Dec 30, 2021 · 6 comments · Fixed by #4269
Closed

Comments

@blogcraft
Copy link

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.

@ajcvickers
Copy link
Member

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

@blogcraft
Copy link
Author

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

@ajcvickers
Copy link
Member

@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;

@ajcvickers ajcvickers 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 Jan 10, 2022
@blogcraft
Copy link
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.

@ErikEJ
Copy link
Contributor

ErikEJ commented Jan 12, 2022

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

@blogcraft
Copy link
Author

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

@ajcvickers ajcvickers self-assigned this Jan 13, 2022
@ajcvickers ajcvickers added this to the Backlog milestone Jan 13, 2022
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Mar 19, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Mar 28, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Mar 30, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
@ajcvickers ajcvickers modified the milestones: Backlog, 8.0.0 Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants