4

我在 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 关系,它工作正常。我无法弄清楚我的映射有什么问题。

4

1 回答 1

3

返回类型不应该是CSList它应该CSList<WorkoutDay>在你的 Regimen 类中,所以改变这个:

    [OneToMany(LocalKey = "ID", ForeignKey = "RegimenID")]
    public CSList WorkoutDays
    {
        get { return (CSList)GetField("WorkoutDays"); }
    }

对此:

    [OneToMany(LocalKey = "ID", ForeignKey = "RegimenID")]
    public CSList<WorkoutDay> WorkoutDays
    {
        get { return (CSList<WorkoutDay>)GetField("WorkoutDays"); }
    }

此处的文档(一对多关系部分下的最后一段):http: //viciproject.com/wiki/Projects/CoolStorage/Doc/UserGuide/Mapping

于 2011-05-12T18:29:03.973 回答