LINQ to SQL: Read entity while calling “SubmitChanges” causes DuplicateKeyException?

I had something like this happen before. Try deleting the Country entity from the DataContext designer and re-adding it.

That did not work. – Dany Leblanc Oct 23 '09 at 13:19.

When you override the default behavior by using ExecuteDynamicInsert|Update|Delete, you should not call SubmitChanges. ExecuteDynamic... implements the change immediately external to the DataContext. You're getting the exception because you are trying to apply the changes again when you call SubmitChanges.

I had this same exception. In my instance I was using instance. ExecuteDynamicUpdate instead of ExecuteDynamicInsert but I don't think it matters.

What does seem to matter is that if you make a change to an object in the object hierarchy within the inset/update you create this error. To me it seems more to do with optimistic concurrency than duplicate keys, but hey, I don't work at Microsoft... I solved my issue by creating a new method in my base business object that called SubmitChanges on the object's data context and then made whatever changes were required. In your case you might try adding a method called Commit to your Country object as follows: public sub Commit(dc as MyDataContext) dc.SubmitChanges() Dim country as Country = dc.Countries.

Where(function (c) c. CountryID = Me. CountryID).Single() end sub Then call c.

Commit instead of datacontext. SubmitChanges in Main.

I solved my issue by creating a new method in my base business object that called SubmitChanges on the object's data context and then made whatever changes were required. Then call c. Commit instead of datacontext.

SubmitChanges in Main.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions