Last post Apr 24, 2017 12:39 PM by SnookerTable
Apr 21, 2017 08:49 PM|SnookerTable|LINK
I am inserting new data in a few different tables with the same data from my main table, but after inserting say 4 or 5 records I get the above error for one of the tables, here is my code
public static void InsertSnookerMainTable(string FirstName, string LastName, string Team)
using (var DB = new FinalWringtonValeDB())
Table1 SnookerMainTable = new Table1();
SnookerMainTable.FirstName = FirstName;
SnookerMainTable.LastName = LastName;
Table2 Div1Breaks = new Table2();
Div1Breaks.FirstName = FirstName;
Div1Breaks.LastName = LastName;
Table3 Div2Breaks = new Table3();
Div2Breaks.FirstName = FirstName;
Div2Breaks.LastName = LastName;
Table4 Contact = new Table4();
Contact.FirstName = FirstName;
Contact.LastName = LastName;
catch (Exception ex)
Error:- ** Violation of PRIMARY KEY constraint . Cannot insert duplicate key **
Apr 23, 2017 01:22 PM|kaushalparik27|LINK
I suggest to debug and Identify which of the table insertion code is throwing this error and check in database, if any of the field for which you are inserting data is defined as Primary Key and being received duplicate values. I suggest to keep an Id or
RecordId kind of field with int/bigint datatype with Identity Seed (1,1) as Primary key to avoid such errors.
Apr 24, 2017 09:13 AM|Zhi Lv - MSFT|LINK
Entity Framework:- Violation of PRIMARY KEY constraint . Cannot insert duplicate key
According to your error message, I think the reason is that the PK of data which you are inserting is already existed in database.
You could debug to check the Message in Exception ex. It will tell you which is duplicate.
Before insert new records into database, you could check whether it is duplicate.
Apr 24, 2017 12:39 PM|SnookerTable|LINK
I have since found the issue it was in another bit of code, but thanks for all your help