Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5976065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:10:30+00:00 2026-05-22T21:10:30+00:00

I have a class with multiple many to many relationships mapping to the same

  • 0

I have a class with multiple many to many relationships mapping to the same secondary class. My EquipmentSet class has two arrays of Equipment objects, and the Equipment class also has an array of EquipmentSets to determine which sets the equipment is a part of.

EF is only generating a lookup table for the second Many to Many relationship. How can I tell EF to generate lookup tables for both? When the code below is used, only the table “ModelSpecificEquipment” is generated. The table “GlobalEquipment” never gets generated.

public partial class EquipmentSet 
{
    public int Id { get; set; }
    public List<Equipment> Global { get; protected set; }
    public List<Equipment> ModelSpecific { get; protected set; }

    public EquipmentSet()
    {
        Global = new List<Equipment>();
        ModelSpecific = new List<Equipment>();
    }
}


public partial class Equipment
{
    public int Id { get; set; }
    public List<EquipmentSet> EquipmentSets { get; set; }

    public Equipment()
    {
    }
}



public class DataContext : DbContext
{
    public DbSet<Equipment>  Equipment { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Equipment>()
            .HasMany<EquipmentSet>(x => x.EquipmentSets)
            .WithMany(x => x.Global)
            .Map(x =>
            {
                x.MapLeftKey("EquipmentId");
                x.MapRightKey("EquipmentSetId");
                x.ToTable("GlobalEquipment");
            });

        modelBuilder.Entity<Equipment>()
            .HasMany<EquipmentSet>(x => x.EquipmentSets)
            .WithMany(x => x.ModelSpecific)
            .Map(x =>
            {
                x.MapLeftKey("EquipmentId");
                x.MapRightKey("EquipmentSetId");
                x.ToTable("ModelSpecificEquipment");
            });
        base.OnModelCreating(modelBuilder);
    }
}

Again, at this point the database that EF creates only contains 3 tables: EquipmentSets, Equipments, ModelSpecificEquipments. GlobalEquipments is missing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:10:30+00:00Added an answer on May 22, 2026 at 9:10 pm

    I think it is not possible to map this. You cannot relate two endpoints on one side to one single endpoint on the other side of a relationship. You will probably need something like this:

    public partial class Equipment
    {
        public int Id { get; set; }
        public List<EquipmentSet> GlobalEquipmentSets { get; set; }
        public List<EquipmentSet> ModelSpecificEquipmentSets { get; set; }
    
        public IEnumerable<EquipmentSet> EquipmentSets
        {
            get
            {
                return GlobalEquipmentSets.Concat(ModelSpecificEquipmentSets);
                // catch cases when one or both of the sets are null.
            }
        }
    }
    

    EquipmentSets is here only a readonly helper which isn’t mapped to the database.

    You can then create a many-to-many relationship between Global and GlobalEquipmentSets and another many-to-many relationship between ModelSpecific and ModelSpecificEquipmentSets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing performance problems with retrieving multiple instances of objects that have many relationships
I have multiple images that have the same class attribute and I want to
I have a 1 to many relationship where each group has multiple positions, i.e.
I have two entities: products and bundles. Each one has its class. A product
I have a class that implements multiple interfaces. I would like to register these
In Django, when you have a parent class and multiple child classes that inherit
I have a C# singleton class that multiple classes use. Is access through Instance
If we have a class that inherits from multiple interfaces, and the interfaces have
I have a java class: it.eng.ancona.view.RuoliView$TabElaborazioneFattureValidazione$ElencoDettaglioElaborazioneFattureValidazione$RigaElencoDettaglioElaborazioneFattureValidazione It's so long for multiple inner class. If
I have a base class with a virtual method, and multiple subclasses that override

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.