gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:09:59 25 4
gpt4 key购买 nike

我正在尝试了解如何为分层数据构建自引用模型。最终我将创建一个类别树。

我还没有表格中的任何内容。确定结构后,我将创建表格。我现有的对象定义如下:

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

我被困在如何使它成为一个我可以发送到 View 以供显示的自引用对象上。我在想 Controller 应该是这样的:

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

这段代码可以编译,我不必更改我的假存储库。我觉得我错过了什么。我不确定如何测试它是否有效。我正在挑战我的知识极限。

我什至不能 100% 确定正确的问题是什么。我要去玩这个...看看我是否收到错误或者它是否按我期望的方式工作。我可能会再次在这里编辑。

编辑2

看来 Category.category 只是返回 null。而且,它似乎不在任何类型的列表中。我不确定我是否正确设置了关系。

有什么想法吗?

最佳答案

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 设计器,则可以创建一个名为“类别”的实体(或类),然后关联回自身。任一设计器都会自动在实体/类上创建一个集合属性,允许您导航到子集合。

您的 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 会“神奇地”为您将“Children”集合设置为“Root”。

关于c# - 如何在 asp.net MVC 中为分层数据构建自引用模型对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3678931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com