Windows phone 7 Windows Phone LINQ到SQL SqlCeException

Windows phone 7 Windows Phone LINQ到SQL SqlCeException,windows-phone-7,linq-to-sql,Windows Phone 7,Linq To Sql,对于学校项目,我们必须制作一个Windows Phone 7.1应用程序。它必须使用本地数据库,我们必须使用LINQ和Bing地图 我们正在制作一个从数据库检索路由的应用程序。每条路线都有一些航路点 现在,我们制作了3个表(更确切地说,但目前只有这3个表是相关的),即“路线”、“航路点”和“RouteWaypointLink”。你可以在下面看到他们的课程 路线: [Table] public class Route : INotifyPropertyChanged, INotifyPropert

对于学校项目,我们必须制作一个Windows Phone 7.1应用程序。它必须使用本地数据库,我们必须使用LINQ和Bing地图

我们正在制作一个从数据库检索路由的应用程序。每条路线都有一些航路点

现在,我们制作了3个表(更确切地说,但目前只有这3个表是相关的),即“路线”、“航路点”和“RouteWaypointLink”。你可以在下面看到他们的课程

路线:

[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;
    }
}
现在奇怪的事情是:当我评论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个RoutewayPointLink时,它确实可以工作,但当我们尝试创建5个RoutewayPointLink时却不能工作,这难道不奇怪吗

当我们尝试添加一些RoutewayPointLink时,我们在程序中也看到了问题的进一步发展(实际上,我们已经走得很远了,但这阻碍了我们前进)

有人知道发生了什么事吗?我们感谢任何帮助

你的,
Tregan

转到服务器资源管理器,然后转到您的IDRoute列属性:

你有这样的选择吗


嗯,我似乎通过添加一些东西来修复它

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;
    }
}
我必须添加包含Route和Waypoint主键的列,这似乎是合乎逻辑的。但是,我仍然不明白为什么当我添加2时它会起作用,但当我添加更多时它不会起作用


现在它已经修复了,所以一切都很好。

在插入两条路由后,您是否尝试调用db.SubmitChanges()?是的,我尝试了,没有改变任何事情。起初我认为这也是因为这个原因,但没有:扫描您通过服务器资源管理器访问您的数据库?没有,回答如下:(嗯,我们获得了Visual Studio 2010 Express的许可证(是的,不是完整版本…),所以我没有服务器资源管理器,很遗憾……:/
/// <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();
    }
}
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 ]
#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();
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");
    }
}