I have two tables in my database 'cable','cablewire'. A composite key exists on the second table for two fields id1, id2. id1 have many-one relationship(with no foreign key association) with the first table's 'cable_id'. Point is that i am not using any association between any of the tables in my database and none of my primary key fields will auto generate.
When i add data into the two tables with primary key values giving manually...
cable newCable = new cable();
newCable.cable_id = newId;
newCable.wireCount = 5;
dbContext.Cables.Add(newCable);
dbContext.SaveChanges();
for(i=0; i<cable.wireCount; i++)
{
cableWire newCableWire = new cableWire();
newCableWire.id1 = newId;
newCableWire.id2 = newId + i + 1;
dbContext.CableWires.Add(newCableWire);
}
dbContext.SaveChanges();
when i call the savechanges after the for loop iteration,it is throwing invalidoperationexception...and here it is.....
I don't know what is going wrong.... Though the composite key(id1+id2) is different for any two records, it is still saying cannot insert duplicate primary key values for the entity type 'cablewire'. Someone please help me what am i doing wrong..
Can't i duplicate one my composite key field value explicitly? I am using Entity Framework 5

Try to put
dbContext.SaveChanges();inside theforloop.