2010-09-09 67 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