[英]Sqlite-net extensions relations always null
I am trying to use the extension in MvvmCross 4. What I am trying to do is simple: I have two tables with a one to many relationship and and want to access this. 我试图在MvvmCross 4中使用扩展。我想要做的很简单:我有两个表有一对多的关系,并且想要访问它。
I have two classes, BusLine
and BusLineGroup
. 我有两个类,
BusLine
和BusLineGroup
。 Each BusLine has one Group as foreign key. 每个BusLine都有一个组作为外键。 What I do in code is run a simple LINQ-Query to get all Buslines:
我在代码中做的是运行一个简单的LINQ-Query来获取所有的Buslines:
var testQuery =
from busLine in this._connection.Table<BusLine>()
select busLine;
The query itself works, but if I check the fields of the returned objects, the Group is always null
!. 查询本身有效,但如果我检查返回对象的字段,则该组始终为
null
! See below for the class and table definitions. 请参阅下面的类和表定义。
What am I doing wrong? 我究竟做错了什么? Why is the group always
null
? 为什么该组始终为
null
? Thanks for your help. 谢谢你的帮助。
The classes in code: 代码中的类:
public class BusLine
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey(typeof(BusLineGroup))]
public int BusLineGroup { get; set; }
[ManyToOne]
public BusLineGroup LineGroup { get; set; }
}
public class BusLineGroup
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public string MainStations { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<BusLine> BusLines { get; set; }
}
The two tables: 两个表:
CREATE TABLE "BusLineGroup" (
`Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`Name` TEXT NOT NULL,
`Color` TEXT NOT NULL,
`MainStations` TEXT NOT NULL
);
CREATE TABLE "BusLine" (
`Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`Name` TEXT NOT NULL,
`BusLineGroup` INTEGER NOT NULL,
FOREIGN KEY(`BusLineGroup`) REFERENCES `BusLineGroup`(`Id`)
);
Installed Nuget-Packages: 已安装的Nuget-Packages:
Note: The MvvmCross package automatically includes SQLite.Net-PCL. 注意: MvvmCross包自动包含SQLite.Net-PCL。 So both of those two use the same PCL.
所以这两者都使用相同的PCL。
You are not using any SQLite-Net Extensions method there, you are using plain sqlite.net methods that know nothing about your relationships. 您没有在那里使用任何SQLite-Net Extensions方法,您使用的是对您的关系一无所知的简单sqlite.net方法。 You have to use the
WithChildren
methods to read and write relationships from database. 您必须使用
WithChildren
方法从数据库读取和写入关系。
For example, your query can be replaced with this line: 例如,您的查询可以替换为此行:
var testQuery = this._connection.GetAllWithChildren<BusLine>();
That will also fetch first-level relationships from database. 这也将从数据库中获取第一级关系。
I'd recommend you to take a look at the SQLite-Net Extensions documentation and the sample project for more examples. 我建议您查看SQLite-Net Extensions文档和示例项目以获取更多示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.