|
-
Apr 4th, 2012, 06:54 AM
#1
Thread Starter
Randalf the Red
[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; }
}
.
Last edited by honeybee; Apr 4th, 2012 at 07:27 AM.
Reason: Found solution
-
Apr 4th, 2012, 08:50 AM
#2
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.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|