Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Entity Framework:- Violation of PRIMARY KEY constraint . Cannot insert duplicate key

$
0
0

Hello

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)
{
try
{
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;

DB.SnookerHandicaps.Add(SnookerMainTable);
DB.Div1Breaks.Add(Div1Breaks);
DB.Div2Breaks.Add(Div2Breaks);
DB.Contacts.Add(Contact);
DB.SaveChanges();
}
}
catch (Exception ex)
{
Error:- ** Violation of PRIMARY KEY constraint . Cannot insert duplicate key **
return;
}
}


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>