c# - 如何使用 SQLite-Net Extensions 遍历多个相关表?

标签 c# sqlite sqlite-net sqlite-net-extensions

我正在尝试使用 SQLite-Net Extensions 遍历多个相关表。但它没有用。我做错了什么?

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

底表:

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 循环中手动获取 child :

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

关于c# - 如何使用 SQLite-Net Extensions 遍历多个相关表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36713839/

相关文章:

objective-c - 可以存储在可执行二进制文件中的文本数据是否有限制?

python - Django + SQLite 如何在发生 "database is locked"错误时增加 SQLite 超时

c# - 查询 bool 列总是返回 false

c# - 关闭 SQLiteAsyncConnection

python - 如何在 Python 中转义 SQLite 表/列名的字符串?

c# - SQLite.Net-PCL 没有这样的功能 : ToString

c# - SQL Server 2008 的 Java 替代方案 - 紧凑

C# 异常处理与方法结果返回错误

c# - 无法转换类型为 'System.Data.Entity.DbSet` 的对象 1[ModelName ]' to type ' System.Data.Entity.DbSet'

c# - Windows Phone 构建应用程序教程