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.
I have two classes, BusLine
and BusLineGroup
. Each BusLine has one Group as foreign key. What I do in code is run a simple LINQ-Query to get all 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
!. See below for the class and table definitions.
What am I doing wrong? Why is the group always 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:
- MvvmCross.Plugin.SQLitePCL
- SQLiteNetExtensions
Note: The MvvmCross package automatically includes SQLite.Net-PCL. So both of those two use the same PCL.