1

I'm new to the fluent API. I have a legacy database which I can't alter at the moment. Simply, this is what I need to achieve:

public class ItemCategory
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Item> Items { get; set; }
}

public class Item
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<ItemCategory> ItemCategories { get; set; }
    public virtual ICollection<Item> RelatedItems { get; set; }

}

Items can be in many categories, RelatedItems can be in different categories to the current Item (which may not have any related items), the existing join tables look like this:

ItemCategoriesItems (ID,ItemCategoryID,ItemID)
RelatedItemCategoriesItems (ID,ItemCategoriesItemsID,RelatedItemCategoriesItemsID)

Hopefully it's obvious that the related items join table above contains 2 foreign keys to the item categories join table - one pointing to the current item and the other to the related item. Currently my onModelCreating code has:

modelBuilder.Entity<ItemCategory>()
    .HasMany(c => c.Items)
    .WithMany(set => set.ItemCategories)
    .Map(mc =>
    {
        mc.ToTable("ItemCategoriesItems","testdb");
        mc.MapLeftKey("ItemCategoryID");
        mc.MapRightKey("ItemID");
    });

... which gets the categories/items working but I'm stuck on how to get the RelatedItems.

Any help greatly appreciated!

Digi
  • 21
  • 1
  • 7
  • I believe this answer provides your solution. http://stackoverflow.com/a/4812413/941058 [1]: http://stackoverflow.com/a/4812413/941058 – kingdango Sep 12 '12 at 13:08

0 Answers0