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

cuongle
  • 74,024
  • 28
  • 151
  • 206
Marty
  • 3,485
  • 8
  • 38
  • 69

1 Answers1

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);
Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
  • 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 – Marty Sep 07 '12 at 13:49
  • 1
    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. – Eoin Campbell Sep 07 '12 at 13:51
  • 3
    You can't have an entity as the junction, and maintain the direct navigation properties. See [this post](http://stackoverflow.com/questions/5418155/many-to-many-relationship-with-junction-table-in-entity-framework) – Mark Oreta Sep 07 '12 at 13:51