I have a big bunch of inserts that I do through DataContext in transaction.
I inserting two types of objects: A and B.
I do execute many sql inserts like:
INSERT INTO A (...) VALUES ('11111111-1111-1111-1111-111111111111',..)
INSERT INTO B (...) VALUES (
'21111111-1111-1111-1111-111111111111',
'11111111-1111-1111-1111-111111111111',...)
separately in
db.ExecuteCommand(sql.ToString(), processedParams.ToArray());
table B have foreign key to A.
Sometimes I get error like:
violation of FOREIGN KEY constraint “FK_B_A” on table “A”. Column “GUID”.
Sometimes all records successfully inserts in to DB. Data bunch always the same except dynamically generated GUIDs.
It looks like the insert in to B sometimes occurs before inserts to A.
Thank you All for help.
It was side effect of bug in transaction cleanup process in my service. It rollback any long very long transaction. Error be thrown when cleanup occurs between insert into table A and table B, becasue record in A no longer existed.