1

学校のプロジェクトでは、Windows Phone 7.1 アプリケーションを作成する必要があります。ローカル データベースを使用する必要があり、これには LINQ と Bing Maps を使用する必要があります。

データベースからルートを取得するアプリケーションを作成しています。各ルートにはいくつかのウェイポイントがあります。

ここで、「Route」、「Waypoint」、「RouteWaypointLink」という 3 つのテーブルを作成しました (実際には、これらの 3 つだけが現時点で関連しています)。以下のクラスを見ることができます

ルート:

[Table]
public class Route : INotifyPropertyChanged, INotifyPropertyChanging
{
    public event PropertyChangingEventHandler PropertyChanging;
    public event PropertyChangedEventHandler PropertyChanged;

    private int idRouteValue;
    private string nameValue;

    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int IDRoute
    {
        get
        {
            return idRouteValue;
        }
        private set
        {
            NotifyPropertyChanging("IDRoute");
            idRouteValue = value;
            NotifyPropertyChanged("IDRoute");
        }
    }
    [Column]
    public string Name
    {
        get
        {
            return nameValue;
        }
        set
        {
            NotifyPropertyChanging("Name");
            nameValue = value;
            NotifyPropertyChanged("Name");
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="propertyName"></param>
    private void NotifyPropertyChanging(string propertyName)
    {
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="propertyName"></param>
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public override string ToString()
    {
        return "[" + IDRoute + "] " + Name;
    }
}

ウェイポイント:

[Table]
public class Waypoint : INotifyPropertyChanged, INotifyPropertyChanging
{
    public event PropertyChangingEventHandler PropertyChanging;
    public event PropertyChangedEventHandler PropertyChanged;

    private int idWaypointValue;
    private string nameValue;
    private double gpsLongitudeValue;
    private double gpsLatitudeValue;
    private string descriptionValue;

    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int IDWaypoint
    { 
        get 
        {
            return idWaypointValue; 
        } 
        private set 
        {
            NotifyPropertyChanging("IDWaypoint");
            idWaypointValue = value;
            NotifyPropertyChanged("IDWaypoint"); 
        }
    }
    [Column]
    public string Name
    {
        get
        {
            return nameValue;
        }
        set
        {
            NotifyPropertyChanging("Name");
            nameValue = value;
            NotifyPropertyChanged("Name");
        }
    }
    [Column]
    public double GPSLongitude 
    {   
        get
        {         
            return gpsLongitudeValue;    
        }  
        set 
        {
            NotifyPropertyChanging("GPSLongitude"); 
            gpsLongitudeValue = value;
            NotifyPropertyChanged("GPSLongitude");  
        }
    }
    [Column]
    public double GPSLatitude
    {
        get
        {
            return gpsLatitudeValue;
        }
        set
        {
            NotifyPropertyChanging("GPSLatitude");
            gpsLatitudeValue = value;
            NotifyPropertyChanged("GPSLatitude");
        }
    }
    [Column]
    public string Description
    {
        get
        {
            return descriptionValue;
        }
        set
        {
            NotifyPropertyChanging("Description");
            descriptionValue = value;
            NotifyPropertyChanged("Description");
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="propertyName"></param>
    private void NotifyPropertyChanging(string propertyName)
    {
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="propertyName"></param>
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return Name;
    }
}

RouteWaypointLink:

[Table]
public class RouteWaypointLink : INotifyPropertyChanged, INotifyPropertyChanging
{
    public event PropertyChangingEventHandler PropertyChanging;
    public event PropertyChangedEventHandler PropertyChanged;

    private int idRouteWaypointLinkValue;
    private EntityRef<Route> routeValue;
    private EntityRef<Waypoint> waypointValue;
    private int waypointIndexValue;

    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int IDRouteWaypointLink
    {
        get
        {
            return idRouteWaypointLinkValue;
        }
        private set
        {
            NotifyPropertyChanging("IDRouteWaypointLink");
            idRouteWaypointLinkValue = value;
            NotifyPropertyChanged("IDRouteWaypointLink");
        }
    }

    [Association(IsForeignKey = true, Storage = "routeValue")]
    public Route Route
    {
        get
        {
            return routeValue.Entity;
        }
        set
        {
            NotifyPropertyChanging("Route");
            routeValue.Entity = value;
            NotifyPropertyChanged("Route");
        }
    }

    [Association(IsForeignKey = true, Storage = "waypointValue")]
    public Waypoint Waypoint
    {
        get
        {
            return waypointValue.Entity;
        }
        set
        {
            NotifyPropertyChanging("Waypoint");
            waypointValue.Entity = value;
            NotifyPropertyChanged("Waypoint");
        }
    }

    [Column]
    public int WaypointIndex
    {
        get
        {
            return waypointIndexValue;
        }
        set
        {
            NotifyPropertyChanging("Index");
            waypointIndexValue = value;
            NotifyPropertyChanged("Index");
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="propertyName"></param>
    private void NotifyPropertyChanging(string propertyName)
    {
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="propertyName"></param>
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return waypointValue.Entity.ToString();
    }
}

プログラムを開始するときに、いくつかのテスト データを入力します。

/// <summary>
/// 
/// </summary>
/// <param name="connection"></param>
public static void MakeDB(string connection)
{
    RallyDatabase db = new RallyDatabase(connection);

    //Temp
    db.DeleteDatabase();

    if (!db.DatabaseExists())
    {
        db.CreateDatabase();

        #region waypoint
        Waypoint w1 = new Waypoint();
        w1.Name = "VVV Breda";
        w1.GPSLatitude = 51.59380;
        w1.GPSLongitude = 4.77963;
        db.WaypointTable.InsertOnSubmit(w1);

        Waypoint w2 = new Waypoint();
        w2.Name = "Liefdeszuster";
        w2.GPSLatitude = 51.59307;
        w2.GPSLongitude = 4.77969;
        db.WaypointTable.InsertOnSubmit(w2);

        Waypoint w3 = new Waypoint();
        w3.Name = "Valkenberg";
        w3.GPSLatitude = 51.59250;
        w3.GPSLongitude = 4.77969;
        db.WaypointTable.InsertOnSubmit(w3);
        #endregion

        #region route
        Route testRoute1 = new Route();
        testRoute1.Name = "Kroegentocht";
        db.RouteTable.InsertOnSubmit(testRoute1);

        Route testRoute2 = new Route();
        testRoute2.Name = "Bezienswaardighedentocht";
        db.RouteTable.InsertOnSubmit(testRoute2);
        #endregion

        #region waypointlinks
        RouteWaypointLink routeWaypointLink1 = new RouteWaypointLink();
        routeWaypointLink1.Route = testRoute1;
        routeWaypointLink1.Waypoint = w1;
        routeWaypointLink1.WaypointIndex = 0;
        db.RouteLinkTable.InsertOnSubmit(routeWaypointLink1);

        RouteWaypointLink routeWaypointLink2 = new RouteWaypointLink();
        routeWaypointLink2.Route = testRoute1;
        routeWaypointLink2.Waypoint = w2;
        routeWaypointLink2.WaypointIndex = 1;
        db.RouteLinkTable.InsertOnSubmit(routeWaypointLink2);

        //Wut, why does it work when this is commented?
        RouteWaypointLink routeWaypointLink3 = new RouteWaypointLink();
        routeWaypointLink3.Route = testRoute1;
        routeWaypointLink3.Waypoint = w3;
        routeWaypointLink3.WaypointIndex = 2;
        db.RouteLinkTable.InsertOnSubmit(routeWaypointLink3);

        RouteWaypointLink routeWaypointLink4 = new RouteWaypointLink();
        routeWaypointLink4.Route = testRoute2;
        routeWaypointLink4.Waypoint = w1;
        routeWaypointLink4.WaypointIndex = 0;
        db.RouteLinkTable.InsertOnSubmit(routeWaypointLink4);

        RouteWaypointLink routeWaypointLink5 = new RouteWaypointLink();
        routeWaypointLink5.Route = testRoute2;
        routeWaypointLink5.Waypoint = w2;
        routeWaypointLink5.WaypointIndex = 1;
        db.RouteLinkTable.InsertOnSubmit(routeWaypointLink5);
        #endregion

        db.SubmitChanges();
    }
}

ただし、これを実行しようとすると、db.SubmitChanges() を指すエラーが発生します。

SqlCeException was unhandled - A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK_RouteWaypointLink_Route ]

奇妙なことに、RouteWaypointLink の最後の 3 つの挿入をコメントすると、正常に動作するため、次のようになります。

#region waypointlinks
RouteWaypointLink routeWaypointLink1 = new RouteWaypointLink();
routeWaypointLink1.Route = testRoute1;
routeWaypointLink1.Waypoint = w1;
routeWaypointLink1.WaypointIndex = 0;
db.RouteLinkTable.InsertOnSubmit(routeWaypointLink1);

RouteWaypointLink routeWaypointLink2 = new RouteWaypointLink();
routeWaypointLink2.Route = testRoute1;
routeWaypointLink2.Waypoint = w2;
routeWaypointLink2.WaypointIndex = 1;
db.RouteLinkTable.InsertOnSubmit(routeWaypointLink2);

//Wut, why does it work when this is commented?
//RouteWaypointLink routeWaypointLink3 = new RouteWaypointLink();
//routeWaypointLink3.Route = testRoute1;
//routeWaypointLink3.Waypoint = w3;
//routeWaypointLink3.WaypointIndex = 2;
//db.RouteLinkTable.InsertOnSubmit(routeWaypointLink3);

//RouteWaypointLink routeWaypointLink4 = new RouteWaypointLink();
//routeWaypointLink4.Route = testRoute2;
//routeWaypointLink4.Waypoint = w1;
//routeWaypointLink4.WaypointIndex = 0;
//db.RouteLinkTable.InsertOnSubmit(routeWaypointLink4);

//RouteWaypointLink routeWaypointLink5 = new RouteWaypointLink();
//routeWaypointLink5.Route = testRoute2;
//routeWaypointLink5.Waypoint = w2;
//routeWaypointLink5.WaypointIndex = 1;
//db.RouteLinkTable.InsertOnSubmit(routeWaypointLink5);
#endregion

db.SubmitChanges();

2 つの RouteWaypointLinks を挿入すると機能するのに、5 つ作成しようとすると機能しないのは変ですか?

また、いくつかの RouteWaypointLinks を追加しようとしたときに、プログラムでさらに問題が発生しました (実際にはかなり遠いですが、これが妨げになっています)。

何が起こっているのか手がかりを持っている人はいますか?どんな助けにも感謝します!

さようなら、トレガン

4

2 に答える 2

0

サーバーエクスプローラーに移動し、IDRoute列のプロパティに移動します。

このようなすべてのオプションがありますか?

ここに画像の説明を入力してください

于 2013-01-09T21:10:22.120 に答える
0

うーん、いくつかのことを追加して修正したようです

private Nullable<int> idRoute;
private Nullable<int> idWaypoint;

[Column(Storage = "idRoute", DbType = "Int")]
public int? IDRoute
{
    get
    {
        return this.idRoute;
    }
    set
    {
        this.idRoute = value;
    }
}

[Column(Storage = "idWaypoint", DbType = "Int")]
public int? IDWaypoint
{
    get
    {
        return this.idWaypoint;
    }
    set
    {
        this.idWaypoint = value;
    }
}

また、関連付けを次のように変更しました。

[Association(IsForeignKey = true, Storage = "routeValue", ThisKey="IDRoute")]
public Route Route
{
    get
    {
        return routeValue.Entity;
    }
    set
    {
        NotifyPropertyChanging("Route");
        routeValue.Entity = value;
        NotifyPropertyChanged("Route");
    }
}

[Association(IsForeignKey = true, Storage = "waypointValue", ThisKey="IDWaypoint")]
public Waypoint Waypoint
{
    get
    {
        return waypointValue.Entity;
    }
    set
    {
        NotifyPropertyChanging("Waypoint");
        waypointValue.Entity = value;
        NotifyPropertyChanged("Waypoint");
    }
}

Route と Waypoint からの主キーを保持する列を追加する必要があるのは当然のことです。それでも、2を追加すると機能する理由がよくわかりませんが、さらに追加すると機能しません。

今は治ったので大丈夫です。

于 2013-01-10T10:52:51.463 に答える