EF Code-First 和嵌套集
我想在我的数据库中创建一个嵌套集模型。但是,我有一个问题,因为我不知道如何使用实体框架代码优先开始实现。
假设我有这样的课程:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嵌套集和对象图是两种完全不同的模型。您不应该将它们存储在一种类型中;这违反了 SRP。
相反,将
Left
和Right
放在代码优先的 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
andRight
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.