views:

16

answers:

0

I have an app which uses Linq2SQL to retrieve parent/child data. The parent data is stored in a binding source, Me.SampleDataBindingSource, and the child data in Me.MeasurementsBindingSource.

When I create a new SampleData entity, the program works fine. But, when I try to retrieve an existing entity, I get the following error:

System.InvalidOperationException: The EntitySet is already loaded and the source cannot be changed.

The error occurs in the Dim tempdata line:

Dim selectedLIMS As String = dgvWorklist.CurrentRow.Cells("LIMSNumber").Value.ToString.Trim
    Dim selectedLot As String = dgvWorklist.CurrentRow.Cells("LotNumber").Value.ToString.Trim

    Try
        Using db As New FiringFactorDataContext
            Dim tempdata = db.SampleDatas.SingleOrDefault(Function(d) d.LIMSNumber = selectedLIMS)
            If tempdata Is Nothing Then
                tempdata = New SampleData
                With tempdata
                    .LIMSNumber = selectedLIMS
                    .LotNumber = selectedLot
                    .DateAdded = Now
                    .RunNo = 1
                End With
            End If
            Me.SampleDataBindingSource.DataSource = tempdata
            Me.MeasurementsBindingSource.DataSource = tempdata
            Me.MeasurementsBindingSource.DataMember = "Measurements"

        End Using

    Catch ex As Exception
        MsgBox(ex.ToString)
        Console.WriteLine(ex.ToString)
    End Try

Any idea why I get this error when I try to retrieve existing data?

UPDATE: I've narrowed the problem. I was using a partial class for the SampleData class to perform various operations on the class data. If I remove the partial class, the problem goes away, but I lose a lot of functionality. For now, I have given up on using the partial class and will use a class that has SampleData as one of its members.