Hello SO,
I'm trying to understand how to build a self referencing model for hierachical data. Eventually i will be creating a category tree.
I don't anything in tables yet. After I have the structure nailed down then I will create the tables. My existing Object is defined like this:
public class Categories
{
public Int64 catID { get; set; }
public Int64? parentCatID { get; set; }
public string catName { get; set; }
public string catDescription { get; set; }
}
My fake repository populates Categories like this:
// Fake hardcoded list of categories
private static IQueryable<Categories> fakeCategories = new List<Categories> {
new Categories { catID = 1, parentCatID = null, catName = "Root", catDescription = "" },
new Categories { catID = 2, parentCatID = 1, catName = "Category w/subs", catDescription = "" },
new Categories { catID = 3, parentCatID = 1, catName = "Category no subs but now has subs", catDescription = "" },
new Categories { catID = 4, parentCatID = 2, catName = "Zub Cat", catDescription = "" },
new Categories { catID = 5, parentCatID = 2, catName = "Sub Cat", catDescription = "" },
new Categories { catID = 6, parentCatID = null, catName = "Another Root", catDescription = "" },
new Categories { catID = 7, parentCatID = null, catName = "Ze German Root", catDescription = "" },
new Categories { catID = 8, parentCatID = 3, catName = "Brand new cats", catDescription = "" },
new Categories { catID = 9, parentCatID = 8, catName = "Brand new cats sub", catDescription = "" },
}.AsQueryable();
I am stuck on how to make this a self referencing object which I can send to a view for display. I was thinking that the controller would be something like this:
public ActionResult CategoryTree()
{
var cats = fakeRepository.Categories.ToList();
return View(cats);
}
I don't know if I'm approaching this correctly. I would display the category tree using a custom HtmlHelper method that recurses.
How would I get Categories
to be a self referencing object?
Edit: rephrased question
I'm starting to think that I'm asking questions that are over my head :-)
Please examine this code:
[Table]
public class Category
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public Int64 catID { get; set; }
public Int64? parentCatID { get; set; }
public string catName { get; set; }
public string catDescription { get; set; }
internal EntityRef<Category> _category;
[Association(ThisKey = "parentCatID", Storage = "_category")]
public Category category {
get { return _category.Entity; }
internal set { _category.Entity = value; }
}
}
This code compiles and I don't have to change my fake repository. I feel like im missing something though. I'm not sure how to test this to see if it works. I'm pushing against the limits of my knowledge.
I'm not even 100% sure of what the correct question is. I'm going to go play with this... see if I get errors or if it works the way i expect. I'll probably edit here again.
Edit 2
It appears that Category.category
is just returning null. And, it doesn't seem to be in a list of any type. I'm not sure if I set up the relationship correctly.
Any thoughts?