为Windows Phone 8应用程序创建本地数据库

时间:2014-04-09 15:42:12

标签: c# linq-to-sql windows-phone-8

我正在尝试为我的WP8应用程序创建一个简单的数据库,以存储一些数据。

[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;
                }
            }
        }       
//...

该协会的另一方:

[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); }
        }
//...

这是我的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;
    }

当我尝试创建数据库时,我得到了一个例外:

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

例外:

Invalid column ID. [ LocationDataId ]

我使用this作为参考,我在代码中看不到什么错误。

其他事情:这是存储此类数据的正确方法,还是应该尝试其他方式?

1 个答案:

答案 0 :(得分:0)

我猜Workout是父表,LocationData是子表。

这个想法是你向子表(对父表的引用)声明EntityRef,向父表声明EntitySet(具有WorkoutID的LocationData项集)。 / p>

Bcareful您的私人领域和公共财产的名称,您在协会中拼错了其中一些。

这就是你的LocationData类中应该声明你的Workout EntityRef关联的方式:

private EntityRef<WorkOut> _workOutRef;

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

Storage指向您的私有EntityRef字段。

ThisKey指向外键公共财产的名称。

IsForeignKey将WorkOutID设置为外键。

在WorkOut类中,您声明一个这样的EntitySet:

private EntitySet<LocationData> _locationsData;

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

Storage指向您的EntitySet私有字段。

OtherKey指向父表的引用键。