I'm creating a system web application similar to a Task list.
I have created a simple parent and child relationship. Once I get past the first level i find it impossible?
I want to be able to view the original parent task and all the other parent and child tasks within that.
So far a Task has an Icollection of tasks.
What I want to be able to do is view all the siblings of the original parent class.
modelBuilder.Entity<Task>()
.HasMany(x => x.Children)
.WithOptional()
.HasForeignKey(x => x.ParentID);
When querying the database do the following:
var tasks = from t in db.Tasks.Include("Parent")
where t.Id //... your logic here
select t;
and you will need to extend your Task class to include the parent property:
public virtual Task Parent { get; set; }
and then to map the relationship:
modelBuilder.Entity<Task>()
.HasOptional(x => x.Parent)
.WithMany(x => x.Children)
.HasForeignKey(x => x.ParentID);
The query above tells EF to bring the parent Task for every returned Task.
Hope this helps.
One solution could be to have to foreign keys. One foreign key is IDRootTask and other foreign key is IDParentTask.
The first IDRootTask is the ID of the first task, so to get all the tasks of the group. The second FK, IDRootParent can be use to get only the task of the parent task. In this way, you don't need a recursive function to get the parents and parents of the parents.
Other option if yo want all the task that are bwlow a determinate task, then you need to store a tree in the database, that allows you to know all the descendants or all the ancestors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.