Thursday, March 11, 2010

Cannot add an entity with a key that ...

Cannot add an entity with a key that is already in use


This ERROR has caused me a lot of aggrevation over the last couple of days as a sniplet that I was using earlier all of a sudden stopped working.

And even when I went back and checked the history it showed no change to that code block.

I read a bunch of articles online (for over 3 days) which led me to believe that the problem was with the LINQ framework and not my code.

Despite strongly disagreeing with the idea to add an IDENTITY column, I finally bit the bullet and added the column but still no luck.

I yelled out in pain "WHY THE HELL WOULD THIS NOT WORK" after a couple of sleepless nights, I tried something else, I tried calling the db.SubmitChanges() function in the begining of my function.

P.S. I am extending my datacontext by using Partial Class

So I had code that looked something like this

Partial Class FancyDataContext
 Public Sub AddYahoo()
  Dim db as FancyDataContext = Me
  Dim NewRecord as New FancyRow
  db.Fancy.InsertOnSubmit( NewRecord )

  db.SubmitChanges() 'THIS IS WHERE IT ERRORED OUT

 End Sub
End Class

I tried variations of my code to see if there was something that I could do to fix this code that was working perfectly fine previously.

AND
What I found was simply AMAZING.
I simply chagned my code from above to

Partial Class FancyDataContext
 Public Sub AddYahoo()
  db.SubmitChanges()

  Dim db as FancyDataContext = Me
  Dim NewRecord as New FancyRow
  db.Fancy.InsertOnSubmit( NewRecord )
  db.SubmitChanges() 'THIS IS WHERE IT ERRORED OUT
 End Sub
End Class

And it errored out.
And Ofcourse, what I noticed was that the function calling my AddYahoo() function was making changes to the datacontext that were not being committed in its block and handing my function the bad data

I did a quick search on LINQ Discard Chagnes and used the article on http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext and created the discard function

Public Sub DiscardInsertsAndDeletes(ByVal data As DataContext)
    ' Get the changes
    Dim changes = data.GetChangeSet()
 
    ' Delete the insertions
    For Each insertion In changes.Inserts
        data.GetTable(insertion.GetType).DeleteOnSubmit(insertion)
    Next
 
    ' Insert the deletions
    For Each deletion In changes.Deletes
        data.GetTable(deletion.GetType).InsertOnSubmit(deletion)
    Next
End Sub

So my code now said 

Partial Class FancyDataContext
 Public Sub AddYahoo()
  DiscardInsertsAndDeletes()

  Dim db as FancyDataContext = Me
  Dim NewRecord as New FancyRow
  db.Fancy.InsertOnSubmit( NewRecord )

  db.SubmitChanges() 'THIS IS NOW WORKING :D YEAY

 End Sub
End Class

So at the end of it all it was human error.

My apologies to the MSFT team for doubting them.

Hope this saves you some aggrevation in the future.

Have an excellent day.












Comments:

Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]