Can't get DB-First model to link to itself with parent/child setup RRS feed

  • Question

  • User-2066416675 posted

    So I'm using this very simple tutorial on jsTree and how to get it to work with MVC (http://www.c-sharpcorner.com/article/c-treeview-to-mvc-razor-view/), It's simple and I got it working.  It creates a simple model that where it has a primary key "ID" and another column "Pid" which is a reference to its own ID field. This way you spit out a list of ID's and Names to create tree nodes and this "Pid" links back to the parent node's ID. It's basically a recursive model that references itself in order to make a tree.  So this guys creates his model as such

    1.   public class Category  
    2.     {  
    3.         //Cat Id  
    4.         public int ID { getset; }  
    6.         //Cat Name  
    7.         public string Name { getset; }  
    9.         //Cat Description  
    10.         public string Description { getset; }  
    12.         //represnts Parent ID and it's nullable  
    13.         public int? Pid { getset; }  
    14.         [ForeignKey("Pid")]  
    15.         public virtual Category Parent { getset; }  
    16.         public virtual ICollection<Category> Childs { getset; }  
    17.     }  
    18. }  ... And sure enough in the home controller when he queries all the parent rows (where Pid is null), I set mybreakpoint on it and I can see it worked. If, while debugging, I pull up his first "Category" entity, I can see that the "Childs" property contains multiple "Childs" entities, one for each record where the Pid of a row matches the ID of that row I'm inspecting.  This allows jsTree to start building the nodes.

    My model is database-first and just as simple, but the only difference is that that I created it by going through the Model wizard and using a stored procedure.. You can't have PK and FK's on stored procedure so I had to add an attribute. Here's my model:

    public partial class Category_Result

    public string ID { get; set; }
    public string ParentID { get; set; }
    public string Description2 { get; set; } 
    public Nullable<bool> Selected { get; set; } // This doesn't really matter now, but I plan on using it to pre-check nodes in the jsTree later
    public virtual Category_Result Parent { get; set; }
    public virtual ICollection<Category_Result> Childs { get; set; }

    As you can see, the model is almost exactly the same, except I had to add the [Key] attribute so EF knows what field to match the ForeignKey to., and my keys are strings instead of ints. When I run this and in my HomeController where I pull up a list all my parent records (ie where ParentID is null), it gets the correct entities from the DB... however when i have my breakpoint set and drill into these parent entities that I'm about to pass to the view, the Childs property for each one is NULL. EF didn't seem to make the "JOIN' or the "LINK" from the foreign key to the primary key.  

    Here is a quick sample of the data returned by my stored procedure

    ParentID ID Description2 Selected
    NULL 1ABCDEFG All Categories 0
    1ABCDEFG 1001 Category 1 0
    1ABCDEFG 1002 Category 2 0
    1ABCDEFG 1003 Category 3 0
    1ABCDEFG 1004A Category 4A 0
    1ABCDEFG 1005A Category 5A 0


    As you can see, there are plenty of entities that have a parent ID that match up exactly to the ID of the first record. But the connection just isn't getting made.  Is there something diferent I need to do with my model, pareticularly on the last line to let EF know what to do?

    Friday, March 9, 2018 4:34 PM

All replies