2016-04-19 3 views
2

Ich versuche, mehrere releated Tabellen mit SQLite-Net Extensions zu durchlaufen. Aber es hat nicht funktioniert. Was mache ich falsch?Wie mehrere releated Tabellen mit SQLite-Net Extensions iterieren?

foreach (var topTableItem in Database.GetAllWithChildren<TopTable>()) 
     { 
      foreach (var middleTableItem in topTableItem.MiddleTableList) 
      { 
       System.Diagnostics.Debug.WriteLine(middleTableItem.Id); // The same database entry as in the working code (see below) 

       System.Diagnostics.Debug.WriteLine(middleTableItem.BottomTableList.Count); // Always return 0 
       foreach (var bottomTableItem in middleTableItem.BottomTableList) 
       { 
        System.Diagnostics.Debug.Write("It works"); // Never reached, because middleTableItem.BottomTableList.Count == 0 
       } 
      } 
     } 

Mit "MiddleTable" als Einstiegspunkt, es funktioniert:

foreach (var middleTableItem in Database.GetAllWithChildren<MiddleTable>()) 
     { 
      System.Diagnostics.Debug.WriteLine(middleTableItem.Id); // The same database entry as in the not working example (see above) 

      System.Diagnostics.Debug.WriteLine(middleTableItem.BottomTableList.Count); // Return > 0 
      foreach (var bottomTableItem in middleTableItem.BottomTableList) 
      { 
       System.Diagnostics.Debug.Write("It works"); // Reached! 
      } 
     } 

TopTable, mit der Liste mit MiddleTable Artikel:

public class TopTable 
{ 
    [PrimaryKey, AutoIncrement] 
    public int Id { get; set; } 

    public string Test { get; set; } = "Test"; 

    [OneToMany(CascadeOperations = CascadeOperation.All)] 
    public List<MiddleTable> MiddleTableList{ get; set; } = new List<MiddleTable>(); 
} 

MiddleTable, mit der Liste mit BottomTable Artikel:

public class MiddleTable 
{ 
    [PrimaryKey, AutoIncrement] 
    public int Id { get; set; } 

    public string Test { get; set; } = "Test"; 

    [OneToMany(CascadeOperations = CascadeOperation.All)] 
    public List<BottomTable> BottomTableList { get; set; } = new List<BottomTable>(); 

    [ForeignKey(typeof(TopTable))] 
    public int TopTableId{ get; set; } 

    [ManyToOne(CascadeOperations = CascadeOperation.All)] 
    public TopTable TopTable{ get; set; } = null; 
} 

BottomTable:

public class BottomTable 
{ 
    [PrimaryKey, AutoIncrement] 
    public int Id { get; set; } 

    public string Test { get; set; } = "Test"; 

    [ForeignKey(typeof(MiddleTable))] 
    public int MiddleTableId { get; set; } 

    [ManyToOne(CascadeOperations = CascadeOperation.All)] 
    public MiddleTable MiddleTable { get; set; } 
} 

Bevor die Schleifen einfügen ich die Objekte:

List<BottomTable> TheList { get; set; } = new List<BottomTable>(); 

     var bottomTableObj = new BottomTable { Test = "ok" }; 
     Database.Insert(bottomTableObj); 

     TheList.Add(bottomTableObj); 

     var middleTableObj = new MiddleTable { Test = "ok", BottomTableList = TheList.ToList() }; 
     Database.Insert(middleTableObj); 

     var middleTableListTemp = new List<MiddleTable>(); 
     middleTableListTemp.Add(middleTableObj); 

     var topTableObj = new TopTable {Test = "ok", MiddleLableList = middleTableList.Temp.ToList()}; 
     Database.Insert(topTableObj); 

     Database.UpdateWithChildren(topTableObj); 

Antwort

0

Mit GetAllWithChildren Sie nur die erste Ebene der Beziehungen zu laden.

Versuchen die recursive Flag auf true Einstellung:

var topTableItems = Database.GetAllWithChildren<TopTable>(recursive: true) 
foreach (var topTableItem in topTableItems) { 

Oder holen die Kinder manuell in der for-Schleife:

foreach (var topTableItem in Database.GetAllWithChildren<TopTable>()) 
{ 
    foreach (var middleTableItem in topTableItem.MiddleTableList) 
    { 
     System.Diagnostics.Debug.WriteLine(middleTableItem.Id); // The same database entry as in the working code (see below) 

     // Fetch MiddleTable children    
     conn.GetChildren(middleTableItem); 

     System.Diagnostics.Debug.WriteLine(middleTableItem.BottomTableList.Count); // Always return 0 
     foreach (var bottomTableItem in middleTableItem.BottomTableList) 
     { 
      System.Diagnostics.Debug.Write("It works"); 
     } 
    } 
}