簡體   English   中英

如何使用SQLite-Net Extensions遍歷多個關聯表?

[英]How to iterate throught multiple releated tables with SQLite-Net Extensions?

我正在嘗試使用SQLite-Net擴展遍歷多個關聯的表。 但這沒有用。 我究竟做錯了什么?

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

以“ MiddleTable”作為入口點,它可以工作:

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,以及帶有MiddleTable項目的列表:

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,以及帶有BottomTable項目的列表:

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

在循環之前,我插入對象:

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

使用GetAllWithChildren只能加載第一級關系。

嘗試將recursive標志設置為true

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

或者在for循環中手動獲取子代:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM