this error actually occurs in two different places withing my web application.
This event fires in code behind file when a user clicks insert link button.
Line of code that fails is below
context.tblUPCCodes.AddObject(upcProfile);
protected void UpcInsertLinkButton_Click(object sender, EventArgs e){
try
{
// Create an entity type
upcProfile = new tblUPCCode();
// Set to false on insert
upcProfile.Obsolete = false;
// Set to empty
upcProfile.Wh_ID = String.Empty;
// Set properties for UPC entity
upcProfile.RTD = cboxRTD.Checked;
upcProfile.Export = cboxExport.Checked;
upcProfile.COORequired = cboxCOO.Checked;
upcProfile.UPCCode = tboxUpcProfileCode.Text.Trim();
upcProfile.AltUPC = tboxUpcProfileAlternateCode.Text.Trim();
upcProfile.GlobalUPC = tboxUpcProfileGlobalNum.Text.Trim();
upcProfile.ShelfLife = Convert.ToInt32(tboxUpcProfileShelfLife.Text.Trim());
upcProfile.ForeignFlavorName = tboxUpcProfileForiegnFlavName.Text.Trim();
upcProfile.Barcode = tboxUpcProfileBarcode.Text.Trim();
// Get primary keys of related entities
CompanyProfilePK = Convert.ToInt32(ddlCompanies.SelectedValue);
BottleProfilePK = Convert.ToInt32(ddlBottles.SelectedValue);
FormulaProfilePK = Convert.ToInt32(ddlFormulas.SelectedValue);
FillerProfilePK = Convert.ToInt32(ddlFillers.SelectedValue);
PalletizerProfilePK = Convert.ToInt32(ddlPalletizers.SelectedValue);
LabelerProfilePK = Convert.ToInt32(ddlLabelers.SelectedValue);
CaseformerProfilePK = Convert.ToInt32(ddlCaseFormers.SelectedValue);
upcProfile.CompanyID = CompanyProfilePK;
upcProfile.BottleTypeID = BottleProfilePK;
upcProfile.FormulaID = FormulaProfilePK;
upcProfile.FillerProfileID = FillerProfilePK;
upcProfile.PalletizerProfileID = PalletizerProfilePK;
upcProfile.LabelerProfileID = LabelerProfilePK;
upcProfile.CaseFormerProfileID = CaseformerProfilePK;
using (var context = new ProductSpecSheetsEntities()){
// Object Set adds entity type to entity
context.tblUPCCodes.AddObject(upcProfile);
// Context Object save entity and inserts new row in DB table
context.SaveChanges();
// Now we can extract the primary key for the inserted record from SQL DB
UpcProfilePK = upcProfile.UpcID;
}
}
catch (Exception exception) {
throw exception.InnerException;
}
As far as I see,I think you'd better check a primary key whether it exists in the DataContext first,and then do inserting。
【Sample】
using (EntityContext ec = new EntityContext())
{
var result = ec.SomeModel.Where(m=>m.PrimaryKey.Equals(your record's id to be inserted)).FirstOrDefault();
if(result==null)
{
//Do inserting here……
}
}
ocsJack
Member
27 Points
31 Posts
Error An item with the same key has already been added.
May 04, 2012 07:34 PM|LINK
this error actually occurs in two different places withing my web application.
This event fires in code behind file when a user clicks insert link button.
Line of code that fails is below
context.tblUPCCodes.AddObject(upcProfile);
protected void UpcInsertLinkButton_Click(object sender, EventArgs e){ try { // Create an entity type upcProfile = new tblUPCCode(); // Set to false on insert upcProfile.Obsolete = false; // Set to empty upcProfile.Wh_ID = String.Empty; // Set properties for UPC entity upcProfile.RTD = cboxRTD.Checked; upcProfile.Export = cboxExport.Checked; upcProfile.COORequired = cboxCOO.Checked; upcProfile.UPCCode = tboxUpcProfileCode.Text.Trim(); upcProfile.AltUPC = tboxUpcProfileAlternateCode.Text.Trim(); upcProfile.GlobalUPC = tboxUpcProfileGlobalNum.Text.Trim(); upcProfile.ShelfLife = Convert.ToInt32(tboxUpcProfileShelfLife.Text.Trim()); upcProfile.ForeignFlavorName = tboxUpcProfileForiegnFlavName.Text.Trim(); upcProfile.Barcode = tboxUpcProfileBarcode.Text.Trim(); // Get primary keys of related entities CompanyProfilePK = Convert.ToInt32(ddlCompanies.SelectedValue); BottleProfilePK = Convert.ToInt32(ddlBottles.SelectedValue); FormulaProfilePK = Convert.ToInt32(ddlFormulas.SelectedValue); FillerProfilePK = Convert.ToInt32(ddlFillers.SelectedValue); PalletizerProfilePK = Convert.ToInt32(ddlPalletizers.SelectedValue); LabelerProfilePK = Convert.ToInt32(ddlLabelers.SelectedValue); CaseformerProfilePK = Convert.ToInt32(ddlCaseFormers.SelectedValue); upcProfile.CompanyID = CompanyProfilePK; upcProfile.BottleTypeID = BottleProfilePK; upcProfile.FormulaID = FormulaProfilePK; upcProfile.FillerProfileID = FillerProfilePK; upcProfile.PalletizerProfileID = PalletizerProfilePK; upcProfile.LabelerProfileID = LabelerProfilePK; upcProfile.CaseFormerProfileID = CaseformerProfilePK; using (var context = new ProductSpecSheetsEntities()){ // Object Set adds entity type to entity context.tblUPCCodes.AddObject(upcProfile); // Context Object save entity and inserts new row in DB table context.SaveChanges(); // Now we can extract the primary key for the inserted record from SQL DB UpcProfilePK = upcProfile.UpcID; } } catch (Exception exception) { throw exception.InnerException; }EntityFramework
Gaspard
Contributor
2066 Points
416 Posts
Re: Error An item with the same key has already been added.
May 04, 2012 08:22 PM|LINK
This web page may help
http://stackoverflow.com/questions/5648060/an-item-with-the-same-key-has-already-been-added
Regards
EntityFramework
ocsJack
Member
27 Points
31 Posts
Re: Error An item with the same key has already been added.
May 04, 2012 08:39 PM|LINK
no luck but thanks for the link. I'm looking for a better understaning of why his error would ever happen and the Entity Framework in general.
ocsJack
Member
27 Points
31 Posts
Re: Error An item with the same key has already been added.
May 04, 2012 09:19 PM|LINK
Error happens here in them model.designer.cs
public ObjectSet<tblUPCCode> tblUPCCodes { get { if ((_tblUPCCodes == null)) { _tblUPCCodes = base.CreateObjectSet<tblUPCCode>("tblUPCCodes"); } return _tblUPCCodes; } }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Error An item with the same key has already been added.
May 06, 2012 02:26 AM|LINK
Hello ocsjack:)
As far as I see,I think you'd better check a primary key whether it exists in the DataContext first,and then do inserting。
【Sample】
using (EntityContext ec = new EntityContext()) { var result = ec.SomeModel.Where(m=>m.PrimaryKey.Equals(your record's id to be inserted)).FirstOrDefault(); if(result==null) { //Do inserting here…… } }