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

时间:2010-09-09 17:19:59

标签: 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; }
}

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

// 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();

我被困在如何使这个自引用对象,我可以发送到视图显示。我以为控制器会是这样的:

public ActionResult CategoryTree()
{
    var cats = fakeRepository.Categories.ToList();
    return View(cats);
}

我不知道我是否正确接近这一点。我将使用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; }
    }
}

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

我甚至不确定正确的问题是什么。我打算玩这个...看看我是否收到错误或者它是否按照我期望的方式运行。我可能会再次在这里编辑。

编辑2

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

有什么想法吗?

1 个答案:

答案 0 :(得分: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; }
}

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

以下是您的Linq2Sql图表的样子:

alt text

以下是协会应该是什么样子: alt text

您的假存储库可以简单地使用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 };

Linq2Sql将'神奇地'为你设置'Root'上的'Children'系列。