2010-09-09 34 views
3

我想了解如何为分层数据构建自引用模型。最终我将创建一个类别树。如何为asp.net MVC中的分层数据构建自引用模型对象?

我什么都没有在表格中。在我确定了结构后,我将创建表格。我现有对象的定义如下:

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); 
} 

我不知道我是否正确接近这个。我会使用递归的自定义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。而且,它似乎不在任何类型的列表中。我不确定我是否正确建立关系。

有什么想法?

+0

你想如何查询你正在使用的数据?它会从最低点起? – WestDiscGolf 2010-09-09 17:37:29

+0

嗯......自上而下??? root-to-subcat -to-subsubcat - etc – quakkels 2010-09-09 18:07:33

回答

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或实体框架设计,而不是你可以创建一个名为“类别”,然后关联到它自身的实体(或类)。任何设计人员都会自动在实体/类上创建一个集合属性,以便您导航到子集合。

这是你的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'收藏集。

+0

我不需要在那里查询父母身份证吗?另外,我不知道我会如何在我的假库中定义它。 – quakkels 2010-09-09 17:30:20

+1

最简单的方法是使用实​​体框架设计器。创建一个类别实体,然后将关联添加到类别与其本身之间的一对多关系。呼叫一端父母和其他孩子。英孚将为您做所有关于智能身份信息的管道工作,并且它会为您提供延迟加载的任何类别的“儿童”和“父母”实体链接。 – 2010-09-09 17:40:17

+0

感谢您的提示。有没有办法让它与linq一起工作? – quakkels 2010-09-09 18:06:54

相关问题