KiteBoarder
KiteBoarder

Reputation: 1

Error code stating "The ObjectContext might be in an inconsistent state."

I am using Visual Studio VB programming against an SQL database utilizing DataAccessLayer & an EDMX file. .NetFramework 4.0 is a resource being used. It simply deletes records from a table called VC_Adjustments in an SQL database & then adds 5 new records to that same table. The table has the 6 fields shown in the code with the first 3 being Primary keys. It runs perfectly the first time it is called but subsequent calls error out as shown below. Here is my code section:

 Private Sub DoWork()
    Dim ID As Integer
    Dim sMsg As String
    ID = 48
    dal.ExecuteStoreCommand(String.Format("DELETE VC_Adjustments FROM VC_Adjustments WHERE VC_Adjustments.CalcID='{0}'", _CalcID))
    dal.SaveChanges()

    For x = 1 To 5
        Dim adj = New WFPDataAccessLayer.VC_Adjustments

        With adj
            .CalcID = ID
            .Section = x
            .Piece = 1
            .StartingFlow = 0
            .StartingPressure = 0
            .DeltaFlow = 0
        End With

        dal.AddToVC_Adjustments(adj)
        dal.SaveChanges()
        adj = Nothing
    Next
End Sub

The first time it runs, it runs perfectly. Subsequent runs produce the following error at the dal.SaveChanges line.

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

Upvotes: 0

Views: 25

Answers (0)

Related Questions