I'm new to using entity framework. I'm using EF5 to insert new data.
I get the dreaded error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_POSTransactionsKitMemberTaxRaw_POSTransactionsKitMemberSaleReturnRaw_KitMemberSaleReturnRowId".
I understand that to mean I don't have a row in [POSTransactionsKitMemberSaleReturnRaw] with a primary key that matches the insert for table [POSTransactionsKitMemberTaxRaw].
I wanted EF to generate the primary keys for me and expected the missing row to have been generated automatically. Here's the code that's failing:
foreach (POSTransactionsKitMemberSaleReturnRaw posTransactionsKitMemberSaleReturnRaw in posTransactionsKitMemberRaw.POSTransactionsKitMemberSaleReturnRaws)
{
++KitMemberSaleReturnRowId;
// set temporary keys
posTransactionsKitMemberSaleReturnRaw.KitMemberSaleReturnRowId = KitMemberSaleReturnRowId;
posTransactionsKitMemberSaleReturnRaw.KitMemberRowId = KitMemberRowId;
repository.AddPOSTransactionsKitMemberSaleReturnRaw(posTransactionsKitMemberSaleReturnRaw);
foreach (POSTransactionsKitMemberTaxRaw posTransactionsKitMemberTaxRaw in posTransactionsKitMemberSaleReturnRaw.POSTransactionsKitMemberTaxRaws)
{
// set temporary keys
posTransactionsKitMemberTaxRaw.KitMemberTaxRowId = ++KitMemberTaxRowId;
posTransactionsKitMemberTaxRaw.KitMemberSaleReturnRowId = KitMemberSaleReturnRowId;
posTransactionsKitMemberTaxRaw.KitMemberKitMemberSaleReturnRowId = null;
repository.AddPOSTransactionsKitMemberTaxRaw(posTransactionsKitMemberTaxRaw);
}
}
I've validated that there are temporary primary key values in the POCO objects at run time. A sql trace of the activity shows the attempted insert value was a generated value (1439630) not the temporary value I set it to.
I've set the Auto detect changes flag off:
_context.Configuration.AutoDetectChangesEnabled = false;
The intent was to improve performance. Since this is a pure insert there should be no way the database rows will change while I am trying to write them.
Any suggestions?