Asked by:
Can't get DB-First model to link to itself with parent/child setup

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
- public class Category
- {
- //Cat Id
- public int ID { get; set; }
- //Cat Name
- public string Name { get; set; }
- //Cat Description
- public string Description { get; set; }
- //represnts Parent ID and it's nullable
- public int? Pid { get; set; }
- [ForeignKey("Pid")]
- public virtual Category Parent { get; set; }
- public virtual ICollection<Category> Childs { get; set; }
- }
- } ... 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
{[Key]
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
[ForeignKey("ParentID")]
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
-
User1120430333 posted
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.
A stored procedure to do what?
Maybe, you could have done the query with Entity-SQL.
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?
Maybe you do it yourself by using E-SQL.
Sunday, March 11, 2018 11:27 AM -
User1400794712 posted
Hi Brian,
It seems you are modifying the model, which is generated with DB First. Just modify the model class, it's not mapped to database, so Childs will always be null. Why don't you modify the table in database directly? It will be easier and more reasonable for DB First.
If you want to modify the model to meet your need, not just modify the model code, but also modify the Diagram model in edmx file. But, the best the way is to modify the table in database directly when we using DB First.
Best Regards,
Daisy
Monday, March 12, 2018 9:24 AM