in Education by
I've searched stackoverflow for a proper solution on generating a many-to-many relationship, using EF Core, Code first and Fluent API. A simple scenario would be: public class Person { public Person() { Clubs = new HashSet(); } public int PersonId { get; set; } public virtual ICollection Clubs { get; set; } } public class Club { public Club() { Persons = new HashSet(); } public int ClubId { get; set; } public virtual ICollection Persons { get; set; } } Please correct me if im wrong but I could honestly not find a question that contains an elaborate explanation on how to do this using the described tools. Can anyone explain how this is done? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
EF Core 5.0 RC1+ As of EF Core 5.0 RC1, it's possible to do this without an explicit join table. EF Core is able to configure a mapping for the many-to-many relationship shown in your question without requiring you to create a PersonClub type. See What's New in EF Core 5.0, RC1, Many-to-many in the official docs for more information. Previous Versions This is not yet possible in EF Core without using an explicit class for the join. See here for an example of how to do that. There's an open issue on Github asking for the ability to do this without the need for an explicit class, but it has not yet been completed. Using your scenario, the example I linked would recommend the following entity classes: public class Person { public int PersonId { get; set; } public virtual ICollection PersonClubs { get; set; } } public class Club { public int ClubId { get; set; } public virtual ICollection PersonClubs { get; set; } } public class PersonClub { public int PersonId { get; set; } public Person Person { get; set; } public int ClubId { get; set; } public Club Club { get; set; } } The following OnModelCreating would then be used for setup: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() .HasKey(pc => new { pc.PersonId, pc.ClubId }); modelBuilder.Entity() .HasOne(pc => pc.Person) .WithMany(p => p.PersonClubs) .HasForeignKey(pc => pc.PersonId); modelBuilder.Entity() .HasOne(pc => pc.Club) .WithMany(c => c.PersonClubs) .HasForeignKey(pc => pc.ClubId); } Be sure to go to the open issue I linked and voice your frustration if you feel the need. EDIT: The open issue suggests using a simple Select to navigate through this somewhat cumbersome hierarchy. In order to get from a PersonId to a collection of Clubs, you can use SelectMany. e.g.: var clubs = dbContext.People .Where(p => p.PersonId == id) .SelectMany(p => p.PersonClubs); .Select(pc => pc.Club); I can't vouch for whether this is truly a "best practice", but it should certainly do the trick and I think its fair to say it's not overly ugly.

Related questions

0 votes
    I'm using EF code first. By convention, I have added a foreign key and a reference to the foreign ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I am trying to update an Entity EF Core record like this: public class Customer { public int Id { ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have an incoming json object that represents the data pulled from a database structure. I would like to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 24, 2022 in Education by JackTerrance
0 votes
    Department and store model. They are a one to one relationship. when I do the (Include) only some ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I want to test my Entities that are built using Entity Framework. My concern is that using Entity Framework ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I try to create database from entity framework code first follow with this tutorial http://www.asp.net/mvc ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I have two tables. Orders OrderID | UserID | OrderTotal 1 | 1 | 100 2 | 2 | 110 3 | 1 ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    What is Database First Approach in MVC using Entity Framework?...
asked Apr 5, 2021 in Education by JackTerrance
0 votes
    I need to add a confirmation entity so I get a 'Yes' or a 'Cancel' in the parameters of a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    How would you test this scenario? I've just started looking into NHibernate and having my first bash at ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Can ASP.NET Core work with the .NET framework?...
asked Feb 16, 2023 in Technology by JackTerrance
0 votes
    @Entity class A { @ManyToMany private List list; ... } @Entity class B { ... } I'd like to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    @Entity class A { @ManyToMany private List list; ... } @Entity class B { ... } I'd like to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Which of the following is a web application API framework? (a) Dojo (b) YUI (c) GWT (d) jQuery ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
...