[RESOLVED] Entity Framework Code First Hierarchical Many To Many Relationship-VBForums
Results 1 to 2 of 2

Thread: [RESOLVED] Entity Framework Code First Hierarchical Many To Many Relationship

  1. #1

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    off others' brains

    Resolved [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:

    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:

    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
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008

    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


Click Here to Expand Forum to Full Width