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

  • SEARCH
  • Home
  • 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 8497189
In Process

The Archive Base Latest Questions

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

I have a many to many relation, and I want to add an intermediate

  • 0

I have a many to many relation, and I want to add an intermediate class, which would enable me adding the many to many relations using repository pattern.

What I can’t figure out is the mapping.

Here’s the structure

public class Product
{
    public Product()
    {
        Categories = new HashSet<Category>();
    }

    public int  Id { get; set; }
    public string Name { get; set; }

    public ICollection<Category> Categories { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public String Name { get; set; }

    public ICollection<Product> Products { get; set; }

}

public class PCMap
{
    public int product_id { get; set; }
    public int category_id { get; set; }

    public Product Product { get; set; }
    public Category Category { get; set; }
}

And the Mapping

modelBuilder.Entity<Product>()
    .HasEntitySetName("PCMap")
    .HasMany(p=>p.Categories)
    .WithMany(p=>p.Products)
    .Map(m=>
        {
            m.MapLeftKey("product_id");
            m.MapRightKey("category_id");
            m.ToTable("PCMap");
        });

modelBuilder.Entity<PCMap>()
    .ToTable("PCMap");

modelBuilder.Entity<PCMap>().HasKey(k => new
    {
        k.category_id,
        k.product_id
    });

modelBuilder.Entity<PCMap>()
    .HasRequired(p=>p.Product)
    .WithMany()
    .HasForeignKey(p => p.product_id);

modelBuilder.Entity<PCMap>()
    .HasRequired(p => p.Category)
    .WithMany()
    .HasForeignKey(p => p.category_id);

Here’s the error that I get..
How do I fix this ?

enter image description here

  • 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-11T00:03:50+00:00Added an answer on June 11, 2026 at 12:03 am

    The way you’ve set it up. PCMap is a non entity and is just used to facilitate the M:N join under the hood.

    Product p = new Product();
    p.Categories ...
    
    Category c = new Category();
    c.Products ...
    

    So beacuse you’ve already defined PC as part of the Product Entity definition here.

    .Map(m=>
            {
                m.MapLeftKey("product_id");
                m.MapRightKey("category_id");
                m.ToTable("PCMap");
            });
    

    I don’t believe you need to (or it’s possible to) define it again, separately below. Try deleting all this code.

    modelBuilder.Entity<PCMap>()
        .ToTable("PCMap");
    modelBuilder.Entity<PCMap>().HasKey(k => new
        {
            k.category_id,
            k.product_id
        });
    
    modelBuilder.Entity<PCMap>()
        .HasRequired(p=>p.Product)
        .WithMany()
        .HasForeignKey(p => p.product_id);
    
    modelBuilder.Entity<PCMap>()
        .HasRequired(p => p.Category)
        .WithMany()
        .HasForeignKey(p => p.category_id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 tables: Book, Section and Content. I want to add many-to-many relation
I have two tables (subjects and pages) in one-to-many relations. I want to add
I have a many to many relation using mongoose, that looks like this. TeamSchema
I have db scheme where Product table have many to many relation to Color
i have an entity Entity1 that have one to many relation with Entity2 as
I have 2 tables having many to many relation and hence there is junction
I have two classes with many-to-many relation so I created a join table in-between
I have just created a new project with a many-to-many relation (User-Group) and the
I have a simple SQL relational model with a many to many relation. Here
I have a question about Criteria method, one-to-many relation to the database, 'one' is

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.