更新或删除时,DataContext.SubmitChanges会导致invalidoperationexception

时间:2013-04-29 00:18:06

标签: vb.net windows-phone-7 one-to-many invalidoperationexception submitchanges

操作系统:Windows Phone 7.5 语言:使用Visual Studio 2010的VB.NET

问题: 我在教师和课程之间有一对多的关系。一位老师可以教一到多门课程。尝试更新或删除时,我会收到invalidoperationexception

如何重现错误:

  1. 点击CreateButton。数据库已创建。
  2. 点击InsertButton。正确插入所有数据。
  3. 点击ViewButton。到目前为止,一切都很好。
  4. 点击Modify。我在这里得到了例外。不知道为什么?
  5. 点击DeleteButton。我在这里得到了同样的例外。不知道为什么?
  6. 以下是代码:

    Partial Public Class MainPage
    Inherits PhoneApplicationPage
    
    ' One Teacher can teach one or more Courses. One-To-Many relationship
    
    Private db As AppContext
    
    Public Sub New()
        InitializeComponent()
    End Sub
    
    Private Sub CreateButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles CreateButton.Click
    
        Try
            db = New AppContext("DataSource=isostore:/CCNB.sdf")
    
            If db.DatabaseExists Then
                db.DeleteDatabase()
            End If
    
            db.CreateDatabase()
    
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    
    End Sub
    
    Private Sub InsertButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles InsertButton.Click
    
        Try
            Dim oTeacher1, oTeacher2 As New Teacher
    
            Dim oCourse1, oCourse2, oCourse3 As New Course
    
            ' Add courses
    
            oCourse1.CourseName = "VB.NET"
            oCourse2.CourseName = "Windows Phone"
            oCourse3.CourseName = "Analyse"
    
            db.Courses.InsertOnSubmit(oCourse1)
            db.Courses.InsertOnSubmit(oCourse2)
            db.Courses.InsertOnSubmit(oCourse3)
    
            ' Add teachers
    
            With oTeacher1
                .TeacherName = "Mike Jones"
                .Courses.Add(oCourse1)
                .Courses.Add(oCourse2)
            End With
    
            With oTeacher2
                .TeacherName = "Sam Roy"
                .Courses.Add(oCourse3)
            End With
    
            db.Teachers.InsertOnSubmit(oTeacher1)
            db.Teachers.InsertOnSubmit(oTeacher2)
    
            db.SubmitChanges()
    
        Catch dbException As Exception
            MessageBox.Show(dbException.Message)
        End Try
    
    End Sub
    
    Private Sub ViewButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ViewButton.Click
    
        Dim Output As New StringBuilder
        Dim TeachersCourses = From oTeacher In db.Teachers
    
        For Each oTeacher In TeachersCourses.ToArray
            Output.Append("Teacher: " & oTeacher.TeacherID & " " & oTeacher.TeacherName & Environment.NewLine)
    
            Output.Append("Number of courses: " & oTeacher.Courses.Count & Environment.NewLine)
            For Each oCourse In oTeacher.Courses
                Output.AppendFormat(vbTab & oCourse.CourseName & Environment.NewLine)
            Next
        Next
    
        MessageBox.Show(Output.ToString)
    
    End Sub
    
    Private Sub DeleteButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles DeleteButton.Click
    
        Try
    
            Dim TeacherIDToDelete As Integer = 2    ' 2 = second teacher
    
            For Each oTeacher In db.Teachers
    
                If oTeacher.TeacherID = TeacherIDToDelete Then
    
                    For Each oCourse In oTeacher.Courses
                        db.Courses.DeleteOnSubmit(oCourse)
                    Next
    
                    db.Teachers.DeleteOnSubmit(oTeacher)
    
                End If
    
            Next
    
            ' Keep getting invalidoperationexception. Reason why: ???
            db.SubmitChanges()
    
            MessageBox.Show(" Deleted")
    
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    
    End Sub
    
    Private Sub UpdateButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles UpdateButton.Click
    
        Try
    
            Dim TeacherIDToModify As Integer = 2    ' 2 = Second teacher
    
            Dim oTeacherModify = (From oTeacher In db.Teachers
              Where oTeacher.TeacherID = TeacherIDToModify
              Select oTeacher).First
    
            oTeacherModify.TeacherName = "John Doe"
    
            ' Keep getting invalidoperationexception. Reason why: ???
            db.SubmitChanges()
    
            MessageBox.Show("Mise à jour effectuée")
    
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    
    End Sub
    
    
    Private Sub CloseButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles CloseButton.Click
    
        db.Dispose()
    
    End Sub
    
    End Class
    
    #Region "Database structure"
    
    Public Class AppContext
    Inherits DataContext
    
    Public Teachers As Table(Of Teacher)
    Public Courses As Table(Of Course)
    
    Public Sub New(ByVal connectionString As String)
        MyBase.New(connectionString)
    End Sub
    
    End Class
    
    <Table(Name:="Teacher")>
    Public Class Teacher
    
    ' Primary Key
    Private TeacherIDInteger As Integer
    
    <Column(DbType:="INT NOT NULL IDENTITY", IsPrimaryKey:=True, IsDBGenerated:=True, CanBeNull:=False, AutoSync:=AutoSync.OnInsert)>
    Public Property TeacherID() As Integer
        Get
            Return TeacherIDInteger
        End Get
        Set(ByVal value As Integer)
            TeacherIDInteger = value
        End Set
    End Property
    
    Private TeacherNameString As String
    
    <Column()>
    Public Property TeacherName() As String
        Get
            Return TeacherNameString
        End Get
        Set(ByVal value As String)
            TeacherNameString = value
        End Set
    End Property
    
    ' One-to-many relationship with Courses
    Private CourseEntitySet As New EntitySet(Of Course)
    
    <Column()>
    <Association(Storage:="CourseEntitySet", ThisKey:="TeacherID", OtherKey:="TeacherID")>
    Public Property Courses() As EntitySet(Of Course)
        Get
            Return Me.CourseEntitySet
        End Get
        Set(value As EntitySet(Of Course))
            Me.CourseEntitySet.Assign(value)
        End Set
    End Property
    
    End Class
    
    <Table(Name:="Course")>
    Public Class Course
    
    ' Primary Key
    Private CourseIDInteger As Integer
    
    <Column(DbType:="INT NOT NULL IDENTITY", IsPrimaryKey:=True, IsDBGenerated:=True, _
     CanBeNull:=False, AutoSync:=AutoSync.OnInsert)>
    Public Property CourseID() As Integer
        Get
            Return CourseIDInteger
        End Get
        Set(ByVal value As Integer)
            CourseIDInteger = value
        End Set
    End Property
    
    Private CourseNametring As String
    
    <Column()>
    Public Property CourseName() As String
        Get
            Return CourseNametring
        End Get
        Set(ByVal value As String)
            CourseNametring = value
        End Set
    End Property
    
    ' Foreign key
    Private TeacherIDInteger As Integer
    
    <Column()>
    Public Property TeacherID() As Integer
        Get
            Return TeacherIDInteger
        End Get
        Set(ByVal value As Integer)
            TeacherIDInteger = value
        End Set
    End Property
    
    End Class
    
    #End Region
    

0 个答案:

没有答案