如何在asp.net MVC中为分层数据构建自引用模型对象?

qua*_*els 3 c# asp.net asp.net-mvc hierarchical-data

我试图了解如何为层次数据构建自引用模型.最终我将创建一个类别树.

我还没有表格中的任何内容.在我将结构固定后,我将创建表.我现有的Object定义如下:

public class Categories
{
    public Int64 catID { get; set; }
    public Int64? parentCatID { get; set; }
    public string catName { get; set; }
    public string catDescription { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的假存储库填充类似这样的类别:

// 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();
Run Code Online (Sandbox Code Playgroud)

我坚持如何使这个自我引用对象,我可以发送到视图显示.我在想控制器是这样的:

public ActionResult CategoryTree()
{
    var cats = fakeRepository.Categories.ToList();
    return View(cats);
}
Run Code Online (Sandbox Code Playgroud)

我不知道我是否正确接近这一点.我将使用recurses的自定义HtmlHelper方法显示类别树.

如何Categories成为自引用对象?

编辑:改述问题

我开始认为我在问我头上的问题:-)

请检查此代码:

[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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码编译,我不必更改我的虚假存储库.我觉得我错过了一些东西.我不知道如何测试它是否有效.我正在推动我的知识极限.

我甚至不确定正确的问题是什么.我打算玩这个......看看我是否有错误,或者它是否按照我的预期运作.我可能会再次在这里编辑.

编辑2

似乎Category.category只是返回null.并且,它似乎不在任何类型的列表中.我不确定我是否正确建立了这种关系.

有什么想法吗?

Ian*_*cer 5

public class Category   // an instance of the class is just ONE category
{
    public Int64 Id { get; set; }
    public Category Parent { get; set; }        // A parent link, EF will  handle this for you using an Association
    public List<Category> Children {get; set;}  // Replace this when you move to EF or L2S
    public string Name { get; set; }
    public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如果您在Visual Studio中使用Linq2Sql或Entity Framework设计器,则可以创建一个名为"Category"的实体(或类),然后创建一个返回自身的关联.设计者将自动在实体/类上创建一个集合属性,允许您导航到子集合.

以下是您的Linq2Sql图表的外观:

替代文字

这就是协会的样子: 替代文字

您的假存储库可以简单地使用Linq2Sql类,如下所示: -

Category root = new Category() { Name = "Root", Parent = null };
Category child1 = new Category() { Name = "Child 1", Parent = root };
Category child2 = new Category() { Name = "Child 2", Parent = root };
Run Code Online (Sandbox Code Playgroud)

并且Linq2Sql将"神奇地"为您设置'Root'上的'Children'集合.