EF Code-First 和嵌套集

发布于 2024-12-07 19:15:25 字数 599 浏览 5 评论 0原文

我想在我的数据库中创建一个嵌套集模型。但是,我有一个问题,因为我不知道如何使用实体框架代码优先开始实现。

假设我有这样的课程:

public class Category
{
    public long Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Category> SubCategories { get; set; }

    // public int left { get; set; }
    // public int right { get; set; }
}

在我的模型中,我需要现在的 SubCategories 集合。但是,当我向子类别添加/删除子类别时,我想实现属性的自动(隐式)更新 集合。

是否可以?

感谢您提前的答复!

最好的!

I would like to create a nested set model inside my database. However, I have a problem, 'cause I don't know how to start the implementation using Entity Framework Code-First.

Lets say I have such class:

public class Category
{
    public long Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Category> SubCategories { get; set; }

    // public int left { get; set; }
    // public int right { get; set; }
}

In my model I need SubCategories collection as it is right now. However I would like to implement automatic (implicit) update of left and right properties when I add/remove subcategories to/from SubCategories collection.

Is it possible?

Thank you for any answer in advance!

Best!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮邮递员 2024-12-14 19:15:25

嵌套集和对象图是两种完全不同的模型。您不应该将它们存储在一种类型中;这违反了 SRP。

相反,将 LeftRight 放在代码优先的 POCO 上,加载,然后将其投影/复制到不同类型的对象上()具有图形模式的实体)。

Nested sets and object graphs are two totally different models. You shouldn't store both of them in one type; that violates SRP.

Instead, put Left and Right on your code-first POCOs, load the, and then project/copy that onto objects of a different type (which are not entities) with a graph schema.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文