[RESOLVED] Entity Framework Code First Hierarchical Many To Many Relationship
I don't know if this should go into the database development, but here it is:
I have a model which essentially is a many to many relationship of itself. Sort of like below:
Code:
class Item
int id;
string name;
IList<Item> ParentItems;
It's basically an item which could be linked to another item as either a parent or a child. So each item could have more than one parent items (shared item) or it could have more than one child items.
I tried creating a database out of this, but there's just a single Item table with an Item_ID property. Normally in a database, this type of a relationship would require two tables, one the Item table and the other a relationship table:
Item: ID, Name
ItemRelation: ItemID, ParentItemID
How do I change the model to create the above setup? At present no matter what I can only do a one to one mapping.
Edit: Found the solution. Here's my model which creates a many to many hierarchical relationship on the same table:
Code:
public class Item
{
public virtual int id { get; set; }
public virtual string name { get; set; }
public virtual IList<Item> ParentItems { get; set; }
public virtual IList<Item> ChildItems { get; set; }
}
.
Re: Entity Framework Code First Hierarchical Many To Many Relationship
Ouch, that's the kind of setup that gives me nightmares.
Please mark the thread as resolved so we know you've found your solution.