4

I am using Vici.CoolStorage in a Windows Phone 7 application to access a SQLite database. I am getting a Null Reference Exception on this line:

 CSList<Regimen> regimens = Regimen.List();

I have two tables in my database:

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)
);

The Mapping for the related models is:

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);}
        }
    }
}

If I comment out the OneToMany relationship in the Regimen model, it works fine. I can't figure out what's wrong with my mapping.

JeffCren
  • 396
  • 1
  • 5
  • 15

1 Answers1

3

The return type shouldn't be CSList it should be CSList<WorkoutDay> in your Regimen class so change this:

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

To this:

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

Documentation here (Last paragraph under the One-to-Many relations section): http://viciproject.com/wiki/Projects/CoolStorage/Doc/UserGuide/Mapping

theChrisKent
  • 15,029
  • 3
  • 61
  • 62