我在 Windows Phone 7 应用程序中使用 Vici.CoolStorage 来访问 SQLite 数据库。我在这一行得到一个空引用异常:
CSList<Regimen> regimens = Regimen.List();
我的数据库中有两个表:
CREATE TABLE Regimen (
ID INTEGER PRIMARY KEY,
Name TEXT(50) NOT NULL
);
CREATE TABLE WorkoutDay (
ID INTEGER PRIMARY KEY,
DayNumber INTEGER NOT NULL,
RegimenID INTEGER REFERENCES Regimen(ID)
);
相关模型的映射是:
using Vici.CoolStorage;
namespace MyApp.Core.Domain
{
[MapTo("Regimen")]
public class Regimen : CSObject<Regimen,int>
{
public int ID
{
get { return (int)GetField("ID"); }
set { SetField("ID", value); }
}
public string Name
{
get { return (string)GetField("Name"); }
set { SetField("Name", value); }
}
[OneToMany(LocalKey = "ID", ForeignKey = "RegimenID")]
public CSList WorkoutDays
{
get { return (CSList)GetField("WorkoutDays"); }
}
}
}
using Vici.CoolStorage;
namespace MyApp.Core.Domain
{
[MapTo("WorkoutDay")]
public class WorkoutDay : CSObject<WorkoutDay,int>
{
public int ID
{
get { return (int)GetField("ID"); }
set { SetField("ID", value); }
}
public int DayNumber
{
get { return (int)GetField("DayNumber"); }
set { SetField("DayNumber", value); }
}
[ManyToOne(LocalKey="RegimenID", ForeignKey="ID")]
public Regimen Regimen
{
get { return (Regimen)GetField("Regimen"); }
set { SetField("Regimen", value);}
}
}
}
如果我在 Regimen 模型中注释掉 OneToMany 关系,它工作正常。我无法弄清楚我的映射有什么问题。