简体   繁体   中英

Creating local database for Windows Phone 8 application

I am trying to create a simple database for my WP8 application, to store some data.

[Table(Name="LocationDatas")]
    public class LocationData
    {

        public int _locationDataId;
        private Nullable<int> _workoutID;
        private EntityRef<Workout> _workoutRef = new EntityRef<Workout>();
        private int _timestamp;
        private double _longitude;
        private double _latitude;
        private double _altitude;
        private double _speed;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false)]
        public int LocationDataIds
        {
            get { return _locationDataId; }
            set { _locationDataId = value; }
        }        

        [Column(Storage = "_workoutID", DbType = "Int")]
        public int? WorkOutId
        {
            get { return _workoutID; }
            set { _workoutID = value; }
        }        

        [Association( Storage = "_workoutRef", ThisKey = "WorkOutId", OtherKey = "LocationDataIds", IsForeignKey = true)]
        public Workout Workout
        {
            get
            {
                return this._workoutRef.Entity;
            }
            set
            {
                this._workoutRef.Entity = value;
                if ((value != null))
                {
                    this.WorkOutId = value.WorkOutId;
                }
            }
        }       
//...

And the other side of the association:

[Table(Name="Workouts")]
    public class Workout 
    {
        private int _workOutId;
        private EntitySet<LocationData> _locationDataIds = new EntitySet<LocationData>();
        private DateTime _date;
        private long _duration;
        private double _distance;
        private double _averageSpeed;
        private int _calories;


        [Column(IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false)]
        public int WorkOutId
        {
            get { return _workOutId; }
            set { _workOutId = value; }
        }


        [Association( Storage = "_locationDataIds", ThisKey = "LocationDataIds", OtherKey = "WorkOutId", IsForeignKey = true)]
        public EntitySet<LocationData> LocationDataIds
        {
            get { return this._locationDataIds; }
            set { this._locationDataIds.Assign(value); }
        }
//...

Here is my DataContext:

public class SportsTrackerDataContext : DataContext
    {
        public static string DBConnectionString ="Data Source=isostore:/SportsTracker.sdf";

        public SportsTrackerDataContext(string connectionString)
            : base(connectionString) { }

        public Table<Workout> workouts;
        public Table<LocationData> locationdatas;
    }

When I'm trying to create the database, I've getting an exception:

using (SportsTrackerDataContext db = new SportsTrackerDataContext(SportsTrackerDataContext.DBConnectionString))
            {
                if (db.DatabaseExists() == false)
                {
                    //Create the database
                    db.CreateDatabase(); //exception thrown here
                    MessageBox.Show("ok");
                }
            }

The Exception:

Invalid column ID. [ LocationDataId ]

I used this as a reference, and I can't see whats wrong in my code.

Other thing: is this a proper way to store such data, or should I try something else?

I'm guessing Workout is the parent table and the LocationData the child table.

The idea is that you declare an EntityRef to the child table (a reference to the parent table) and an EntitySet to the parent table (the set of LocationData items with a WorkoutID).

Bcareful with the names of your private fields and your public properties, you have misspelled some of them in your associations.

That's the way your Workout EntityRef association should be declared in your LocationData class:

private EntityRef<WorkOut> _workOutRef;

[Association(
    Storage = "_workOutRef", ThisKey = "WorkOutID", IsForeignKey = true)]
public WorkOut WorkOut
{
    ...
}

Storage points to your private EntityRef field.

ThisKey points to the name of your foreign key public property.

IsForeignKey sets WorkOutID as the foreign key.

On the WorkOut class, you declare an EntitySet like this:

private EntitySet<LocationData> _locationsData;

[Association(
    Storage = "_locationsData", OtherKey = "WorkOutID")]
public EntitySet<LocationData> LocationsData
{
    ...
}

Storage points to your EntitySet private field.

OtherKey points to the referenced Key of the parent table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM