I am trying to understand how to create a self-relational model for hierarchical data. In the end I will create a category tree.
I still don’t see anything in the tables. After I cover the structure, I will create the tables. My existing object is defined as follows:
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 the following 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 went in cycles on how to make this object for self-binding, which I can send for viewing for display. I thought the controller would be something like this:
public ActionResult CategoryTree()
{
var cats = fakeRepository.Categories.ToList();
return View(cats);
}
I do not know if I approached correctly. I would display the category tree using the custom HtmlHelper method, which will be recurses.
How can I get Categoriesas an object of self-regulation?
Edit: rephrased question
, , : -)
, :
[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; }
}
}
, . , - . , , , . .
100% . ... , , . , , .
2
, Category.category null. , , . , .
?