C# 如何使用SQLite网络扩展迭代多个相关表?
我试图用SQLite网络扩展迭代多个相关表。但它不起作用。我做错了什么C# 如何使用SQLite网络扩展迭代多个相关表?,c#,sqlite,sqlite-net,sqlite-net-extensions,C#,Sqlite,Sqlite Net,Sqlite Net Extensions,我试图用SQLite网络扩展迭代多个相关表。但它不起作用。我做错了什么 foreach (var topTableItem in Database.GetAllWithChildren<TopTable>()) { foreach (var middleTableItem in topTableItem.MiddleTableList) { System.Diagnostics.Debug
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
}
}
}
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>();
}
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 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>();
}
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;
}
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);
List列表{get;set;}=new List();
var bottomTableObj=新的BottomTable{Test=“ok”};
数据库.Insert(bottomTableObj);
添加列表(底部表格对象);
var middleTableObj=new MiddleTable{Test=“ok”,BottomTableList=TheList.ToList()};
数据库.插入(middleTableObj);
var middleTableListTemp=新列表();
middleTableListTemp.Add(middleTableObj);
var topTableObj=newtoptable{Test=“ok”,MiddleLableList=middleTableList.Temp.ToList();
数据库.插入(topTableObj);
数据库.UpdateWithChildren(topTableObj);
使用GetAllWithChildren
只加载第一级关系
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>();
}
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;
}
尝试将recursive
标志设置为true
:
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>();
}
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;
}
var topTableItems = Database.GetAllWithChildren<TopTable>(recursive: true)
foreach (var topTableItem in topTableItems) {
var-topTableItems=Database.GetAllWithChildren(递归:true)
foreach(topTableItems中的变量topTableItem){
或者在for循环中手动获取子对象:
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>();
}
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;
}
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");
}
}
}
foreach(数据库中的var topTableItem.GetAllWithChildren())
{
foreach(topTableItem.MiddleTableList中的变量middleTableItem)
{
System.Diagnostics.Debug.WriteLine(middleTableItem.Id);//与工作代码中的数据库条目相同(见下文)
//获取中间表子项
conn.GetChildren(中间表项目);
System.Diagnostics.Debug.WriteLine(middleTableItem.BottomTableList.Count);//始终返回0
foreach(middleTableItem.BottomTableList中的变量bottomTableItem)
{
System.Diagnostics.Debug.Write(“它工作”);
}
}
}