2

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 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, deleting this code, eliminates the error. My problem is - that I need access to that table (PCMap). What I want is to be able to directly Instantiate the PCMap instance and save it to the DB
ah. Then you don't want to create the mapping the way you've done. Instead you want to have a relationship like Product.PCMaps.Categories and Category.PCMaps.Products. I don't think you can have it both ways.
You can't have an entity as the junction, and maintain the direct navigation properties. See this post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.