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 8504521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:03:26+00:00 2026-06-11T02:03:26+00:00

I have a table called Products which obviously contains products. However, I need to

  • 0

I have a table called Products which obviously contains products.
However, I need to create related products. So what I’ve done is create a junction table called product_related which has two PKs. ProductID from Products table and RelatedID also from Products table.

I already use EF and have set up everything on other tables. How should I add this properly in order to create a relationship with products as such:
product.Products.Add(product object here). Of course here product represent a product object that I’ve fetched from the db using db.Products.FirstOr....

How should I do this properly ? A many to many to the same table?

Thanks.

  • 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-06-11T02:03:28+00:00Added an answer on June 11, 2026 at 2:03 am

    In order to create a many-to-many relationship with Database-First approach you need to setup a database schema that follows certain rules:

    • Create a Products table with a column ProductID as primary key
    • Create a ProductRelations table with a column ProductID and a column RelatedID and mark both columns as primary key (composite key)
    • Don’t add any other column to the ProductRelations table. The two key columns must be the only columns in the table to let EF recognize this table as a link table for a many-to-many relationship
    • Create two foreign key relationships between the two tables:
      • The first relationship has the Products table as primary-key-table with the ProductID as primary key and the ProductRelations table as foreign-key-table with only the ProductID as foreign key
      • The second relationship also has the Products table as primary-key-table with the ProductID as primary key and the ProductRelations table as foreign-key-table with only the RelatedID as foreign key
    • Enable cascading delete for the first of the two relationships. (You can’t do it for both. SQL Server won’t allow this because it would result in multiple cascading delete paths.)

    If you generate an entity data model from those two tables now you will get only one entity, namely a Product entity (or maybe Products if you disable singularization). The link table ProductRelations won’t be exposed as an entity.

    The Product entity will have two navigation properties:

    public EntityCollection<Product> Products { get { ... } set { ... } }
    public EntityCollection<Product> Products1 { get { ... } set { ... } }
    

    These navigation collections are the two endpoints of the same many-to-many relationship. (If you had two different tables you wanted to link by a many-to-many relationship, say table A and B, one navigation collection (Bs) would be in entity A and the other (As) would be in entity B. But because your relationship is “self-referencing” both navigation properties are in entity Product.)

    The meaning of the two properties are: Products are the products related to the given product, Products1 are the products that refer to the given product. For example: If the relationship means that a product needs other products as parts to be manufactured and you have the products “Notebook”, “Processor”, “Silicon chips” then the “Processor” is made of “Silicon chips” (“Silicon chips” is an element in the Products collection of the Processor product entity) and is used by a “Notebook” (“Notebook” is an element in the Products1 collection of the Processor product entity). Instead of Products and Products1 the names MadeOf and UsedBy would be more appropriate then.

    You can safely delete one of the collections from the generated model if you are only interested in one side of the relationship. Just delete for example Products1 in the model designer surface. You can also rename the properties. The relationship will still be many-to-many.

    Edit

    As asked in a comment the model and mapping with a Code-First approach would be:

    Model:

    public class Product
    {
        public int ProductID { get; set; }
    
        public ICollection<Product> RelatedProducts { get; set; }
    }
    

    Mapping:

    public class MyContext : DbContext
    {
        public DbSet<Product> Products { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>()
                .HasMany(p => RelatedProducts)
                .WithMany()
                .Map(m =>
                {
                    m.MapLeftKey("ProductID");
                    m.MapRightKey("RelatedID");
                    m.ToTable("product_related");
                });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called products, which contains two rows (plus others):price and mfr.
I have a table called Products in my database. It has a couple of
i have table called as Support, which have a field named Name and contains
Hi I have table called Sold Products which stores buying information for product &
I have a table called order which has many order_items , each order_items is
Hay, I have a table a products which has a 'cost_price' field. When a
I have a table called products, and a mapping table called related_products which maintains
I have a MySql table called reviews which stores a lot of product reviews,
I have a table called products containing a field called price and I simply
I have a products table with a column of type SET (called especialidad), with

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.