8

我有一个引用自身的表,但我正在努力获得我想要的映射。我希望能够将孩子定义为具有特定人作为母亲、父亲和/或监护人的人的集合。监护人可以是父亲或母亲。

我想要一个可以在列出人员的地方浏览的人员树形视图;用户可以展开一个人的节点以显示该人的所有孩子,而不考虑孩子定义的关系(母亲、父亲或监护人)。

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>
4

3 回答 3

5

我想更好的解决方案是在您的模型中创建三个导航列表,并且可能有一种方法来加入对象以将所有儿子返回给您。

例如

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;
}

但是您应该始终注意将它们包含在您的查询中,例如

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();
于 2016-01-08T21:08:09.773 回答
1
  1. 所有的表数据都应该在手边(如果没有,我们可能会多次调用数据库)。
  2. 查找所有没有父母的人的列表(即一个人没有监护人、母亲、父母)并从他们开始部分。
于 2016-01-06T04:55:01.057 回答
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>
于 2016-01-12T09:45:51.713 回答