0

I use LINQ to SQL. I have simple one attribute table in SQL Server, this attribute is also primary key. Here is the table structure (screenshot from Visual Studio 2012)

enter image description here

I use linq to create corresponding entities from List<string>.

This entity's one and only attribute is token. I traverse through the List<string> (this is deserialized from a xml file), and create Token entity and set its token attribute to a string from List and call InsertOnSubmit.

After populating the Token table, I call SubmitChanges on context. At this point I get error that I have primary key violation on some string, let it be bla bla.

I've checked several times inside my xml file (from which I've deserialized my List<string>) that I have only one bla bla. I've tried to remove this bla bla from string, after I do that, I get the same error on other string, and again check the file and again I have only one of that string inside the file.

I even checked inside the code the count of elements which contain string bla bla, and again there count is only 1. I don't know from where LINQ takes another instance of "bla bla" and I got really angry. Of course I've checked that I call InsertOnSubmit only one time inside my code for every string. So I have no idea why I continue to get this primary key violation on "bla bla".

Any suggestions??

Thanks for the answers

kyurkchyan
  • 2,260
  • 2
  • 23
  • 37
  • Can you add the structure of the table? Does it have an auto-incremented ID? – Giannis Paraskevopoulos Aug 17 '13 at 10:41
  • I've added screenshot please check it out. No it hasn't any auto increment. Also I've check there is not any data inside table, so I insert into blank table. – kyurkchyan Aug 17 '13 at 10:48
  • I suggest you would have a look at http://stackoverflow.com/questions/6796009/violation-of-primary-key-constraint – Giannis Paraskevopoulos Aug 17 '13 at 11:02
  • I've checked that, but in my case it will be to slow, I have lot's of records and checking for every one is to slow, instead I just guarantee that values are unique before inserting. And I use primary key just to be able define foreign key later and my db won't be modified later, so I need primary key only for defining foreign key. – kyurkchyan Aug 17 '13 at 11:27
  • The values you are adding clearly are not unique. – Mark Homer Aug 17 '13 at 18:04

0 Answers0