如何建模层次结构 [英] How to Model Hierarchies
问题描述
我想对任务组建模,以便一个任务可以有多个子任务,一个子任务可以分配给许多任务。
我的实体看起来像这样:
公共课程任务
{
  public long Id {get;组; }
  public string Name {get;组; }
}
公共类任务组
{
  public long Id {get;组; }
  public long ParentTaskId {get;组; }
  [ForeignKey(" ParentTaskId")]
$
  public virtual Task ParentTask {get;组; }
  public long ChildTaskId {get;组; }
  [ForeignKey(" ChildTaskId")]
$
  public virtual Task ChildTask {get;组; }
  public byte Level {get;组; }
}
我明确定义交叉点实体,因为我必须保留额外信息(等级)。
我是否必须在enitity Task中声明如下内容,以便我能够进行查询
,例如"db.Tasks.Where(t) => t.ParentTasks.Count == 0);"在没有父母的情况下完成任务?
&NBSP; public virtual ICollection< TaskGroup> ParentTasks {get;组; }¥b $ b &NBSP; public virtual ICollection< TaskGroup> ChildTasks {得到;组; }
如何在EF中建立这样的层次结构。
我尝试了很多,但没有工作得非常满意。
非常感谢。
I want to model groups of tasks so that one task can have many subtasks and one subtask can be assigned to many tasks.
My Entities look like this:
public class Task
{
public long Id { get; set; }
public string Name { get; set; }
}
public class TaskGroup
{
public long Id { get; set; }
public long ParentTaskId { get; set; }
[ForeignKey("ParentTaskId")]
public virtual Task ParentTask { get; set; }
public long ChildTaskId { get; set; }
[ForeignKey("ChildTaskId")]
public virtual Task ChildTask { get; set; }
public byte Level { get; set; }
}
I explicitly define the intersection entity as I have to persist extra information (Level).
Do I have to declare something like the following in enitity Task, so that I'm able to make queries
such as "db.Tasks.Where(t => t.ParentTasks.Count == 0);" to get the tasks with no parent?
public virtual ICollection<TaskGroup> ParentTasks { get; set; }
public virtual ICollection<TaskGroup> ChildTasks { get; set; }
How can you model such a hierarchy in EF.
I tried a lot, but nothing worked satisfactorily.
Thanks a lot.
推荐答案
罗马T öngi,
>>我是否必须在enitity Task中声明类似以下的内容,以便我能够进行查询
这样的as"db.Tasks.Where(t => t.ParentTasks.Count == 0);"得到没有父母的任务?
>>Do I have to declare something like the following in enitity Task, so that I'm able to make queries
such as "db.Tasks.Where(t => t.ParentTasks.Count == 0);" to get the tasks with no parent?
根据你的描述,似乎我们可以在任务类中添加一个字段,如下所示:
Based on your description, it seems that we could add a field in task class, like this:
public class Task
{
public long Id { get; set; }
public string TaskType { get; set; }
public string Name { get; set; }
}
然后你可以像这样过滤ParentTask:
Then you could filter ParentTask like this:
db.Tasks.Where(t => t.TaskType == "Parent");
祝你好运,
Cole Wu
这篇关于如何建模层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!