8

Mam tabelę, która się odwołuje, ale mam problem z uzyskaniem pożądanego odwzorowania. Chcę móc zdefiniować Dzieci jako zbiór ludzi, którzy mają określoną osobę jako Matka, Ojciec i/lub Opiekun. Opiekun może być ojcem lub matką.Tabela samodzielnego odniesienia EF6 z ​​wieloma właściwościami macierzystymi, pojedyncza kolekcja dla dzieci

Chcę mieć widok drzew osób, które można przeglądać w miejscach, w których znajdują się ludzie; użytkownik może rozwinąć węzeł osoby, aby pokazać wszystkie dzieci tej osoby, niezależnie od relacji określającej dziecko (matka, ojciec lub opiekun).

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

Każda pomoc będzie mile widziane w tej chwili moim zdaniem ma wyglądać tak:

@using Person_MVC.Models 
    @model IEnumerable<Person> 
    @{ 
     IEnumerable<Person> children; 
    } 

    <ul> 
     @foreach (Person person in Model.OrderBy(p => p.PersonNumber)) 
     { 
      <li id="[email protected]" 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> 

Odpowiedz

5

Sądzę, że lepszym rozwiązaniem jest utworzenie w swoim modelu trzech list nawigacyjnych i może istnieć jedna metoda łączenia obiektów w celu zwrócenia wszystkich synów.

np.

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

ale zawsze należy zadbać, aby uwzględnić je w swojej zapytań np

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. Wszystkie dane tabeli powinny być w ręku (Jeśli nie możemy mieć wiele połączeń do bazy danych) .
  2. Znajdź listę wszystkich osób, które nie mają rodziców (tj. Bez opiekuna, matki, rodziców dla danej osoby) i rozpocznij częściowe z nimi.
0

Try To może zbyt ...

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="[email protected]" 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>