none
System.Data.Linq.DuplicateKeyException problem RRS feed

  • Question

  • Hi,

    The DataContext is unique, in the web application project it's a "Main" controler's classe property "_dc"? the constructor is :
    Code Snippet

    If _dc Is Nothing Then

    _dc = New CovoiturageDataContext

    End If


    In my Web application project, there is a "subscribe" page. An employee (Collaborateur) is identify in the SQL Server table by his company ID (matricule), a 6 digits integer : Primary Key, int, Not Null, no Identity (of course).

    When the user valid the page, the controler classe is called with the page's web control values as parameters.

    A method classe is in charge of verifiing this ID is not in the RDBMS :
    Code Snippet

    dc = CtrlMain.dc

    Dim monCollaborateur = (From c In dc.Collaborateur _
                            Where c.matricule = leMatricule _
                            Select c).SingleOrDefault()
    return monCollaborateur


    So if the method returns Nohing, I create a new Collaborateur, set his properties, Ok.

    I verify in the SQL Server Management Studio in the Database, with a "SELECT ... WHERE ID =...", no records found, Ok.

    So dc.InsertOnSubmit() everything works fine.

    But, almost once of 2 tests, the insert dc.SubmitChanges() method return a System.Data.Linq.DuplicateKeyException

    So if the record is not in the Database, another one could be in memory, but I didn't create it ?!!

    Any idea ?

    Thanks,
    JC
    Tuesday, November 25, 2008 9:22 AM

Answers

  • Hi,

    So I couldn't remove a record from the cache.

    No work around found on the net.

    ==> I remove all my InsertOnSubmit, DeleteOnSubmit.... SubmitChanges method calls.

    I create an Insert and an update method with the good olds ADO.NET connected methods and performs a Refresh on the entity object (refresh the cache with the Database values).

     JC
    Wednesday, November 26, 2008 1:08 PM

All replies

  • The test Code which throws the exception on the 2nd insert :

            'CREATE TABLE Test(
    ' id int NOT NULL PRIMARY KEY,
    ' libelle nchar(10) NOT NULL)
     
    Private Sub CmdButtonTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdButtonTest.Click
    Dim dc As CovoiturageDataContext = Nothing
     
    Try
    'Singleton DataContext
    ctrlM = CtrlMain.GetInstance()
    dc = ctrlM.dc
    Dim TheTest = (From t In dc.Test Select t Where t.id = 1).FirstOrDefault
    If TheTest Is Nothing Then
    System.Diagnostics.Debug.WriteLine("No records in the Database's Test table with id=1")
    TheTest = New Test
    TheTest.id = 1
    TheTest.libelle = "aaa"
    dc.Test.InsertOnSubmit(TheTest)
    dc.SubmitChanges()
    Else
    System.Diagnostics.Debug.WriteLine("One record in the Database's Test table with id=1")
    End If
     
    'BreakPoint here and 'DELETE FROM Test WHERE id=1' in SQL Server Management Studio
     
    TheTest = (From t In dc.Test Select t Where t.id = 1).FirstOrDefault
    If TheTest Is Nothing Then
    System.Diagnostics.Debug.WriteLine("No records in the Database's Test table with id=1")
    TheTest = New Test
    TheTest.id = 1
    TheTest.libelle = "aaa"
    dc.Test.InsertOnSubmit(TheTest)
    dc.SubmitChanges()
    Else
    System.Diagnostics.Debug.WriteLine("One record in the Database's Test table with id=1")
    End If
     
    Catch ex As Exception
    System.Diagnostics.Debug.WriteLine("Error : " & ex.ToString)
    End Try
    End Sub

    Tuesday, November 25, 2008 1:53 PM
  • Hi,

    So I couldn't remove a record from the cache.

    No work around found on the net.

    ==> I remove all my InsertOnSubmit, DeleteOnSubmit.... SubmitChanges method calls.

    I create an Insert and an update method with the good olds ADO.NET connected methods and performs a Refresh on the entity object (refresh the cache with the Database values).

     JC
    Wednesday, November 26, 2008 1:08 PM