我有一个引用自己的表,但我正在努力获得我想要的映射。我希望能够将儿童定义为拥有某个人作为母亲,父亲和/或监护人的人的集合。守护者可能是父亲或母亲。
我希望能够看到人们被列入名单的人们可以浏览的树状图;用户可以扩展一个人的节点以显示所有该人的孩子,无论儿童定义的关系(母亲,父亲或监护人)如何。
public partial class Person
{
[Key]
public int ID { get; set; }
[StringLength(100)]
public string Name { get; set; }
public int? GuardianID { get; set; }
[Column("MotherID")]
public int? MotherID { get; set; }
[Column("FatherID")]
public int? FatherID { get; set; }
[ForeignKey("MotherID")]
public virtual tblPerson Mother { get; set; }
[ForeignKey("FatherID")]
public virtual tblPerson Father { get; set; }
[ForeignKey("GuardianID")]
public virtual tblPerson Guardian { get; set; }
[InverseProperty("Guardian")]
[InverseProperty("Father")]
[InverseProperty("Mother")]
public virtual IEnumerable<tblPerson> children { get; set; }
}
现在我的观点必须如下所示:
@using Person_MVC.Models
@model IEnumerable<Person>
@{
IEnumerable<Person> children;
}
<ul>
@foreach (Person person in Model.OrderBy(p => p.PersonNumber))
{
<li id="Pnl_@Person.ID" data-jstree='{"type":"Person"}' data-Personkey="@Person.ID.ToString()">
@Person.Name
@{
PersonModel db = new PersonModel();
children = (from p in db.Persons where p.GuardianID == Person.ID || p.Father == Person.ID || p.MotherID == Person.ID select p).ToList();
}
@if (children != null && children.Count() > 0)
{
@Html.Partial("PersonTree", children)
}
</li>
}
</ul>
答案 0 :(得分:5)
我想更好的解决方案是在模型中制作三个导航列表,并且可以使用一种方法来连接对象以将所有儿子返回给您。
e.g。
public int? FatherId { get; set; }
public int? GrandFatherId { get; set; }
public int? MotherId { get; set; }
public virtual ICollection<Person> FatherForThose { get; set; }
public virtual Person Father { get; set; }
public virtual ICollection<Person> GrandFatherForThose { get; set; }
public virtual Person GrandFather { get; set; }
public virtual ICollection<Person> MotherForThose { get; set; }
public virtual Person Mother { get; set; }
public ICollection<Person> GetChildren()
{
var list = FatherForThose.Concat(MotherForThose).ToList();
foreach (var person in GrandFatherForThose)
{
if (list.All(i => i.Id != person.Id))
{
list.Add(person);
}
}
return list;
}
但是你应该始终注意将它们包含在你的查询中 e.g。
var grand = context.Persons.Include(x => x.FatherForThose)
.Include(x => x.GrandFatherForThose)
.Include(x => x.MotherForThose)
.FirstOrDefault(x => x.Id == 2);
var list = grand.GetChildren();
答案 1 :(得分:1)
答案 2 :(得分:0)
也可以试试这个......
public partial class Person
{
[Key]
public int ID { get; set; }
[StringLength(100)]
public string Name { get; set; }
public int? GuardianID { get; set; }
[Column("MotherID")]
public int? MotherID { get; set; }
[Column("FatherID")]
public int? FatherID { get; set; }
public IEnumerable<Person> Children { get
{
return context.Person.Where(p => p.GuardianID == this.ID || p.Father == this.ID || p.MotherID == this.ID).ToList();
}
}
}
@using Person_MVC.Models
@model IEnumerable<Person>
<ul>
@foreach (Person person in Model.OrderBy(p => p.PersonNumber))
{
<li id="Pnl_@Person.ID" data-jstree='{"type":"Person"}' data-Personkey="@Person.ID.ToString()">
@Person.Name
@if (Person.Children != null && Person.Children.Count() > 0)
{
@Html.Partial("PersonTree", Person.Children)
}
</li>
}
</ul>