通过 System.Data.Linq.DataContext 插入 SQL Server 时出错
我通过事务中的 DataContext 进行了大量插入。 我插入两种类型的对象:A和B。
我确实执行了许多sql插入,例如:
INSERT INTO A (...) VALUES ('11111111-1111-1111-1111-111111111111',..)
INSERT INTO B (...) VALUES (
'21111111-1111-1111-1111-111111111111',
'11111111-1111-1111-1111-111111111111',...)
分别在
db.ExecuteCommand(sql.ToString(), processedParams.ToArray());
表B中具有指向A的外键。
有时我会收到如下错误: 违反表“A”上的外键约束“FK_B_A”。列“GUID”。
有时所有记录都成功插入到数据库中。除了动态生成的 GUID 之外,数据束始终相同。
看起来有时插入 B 会发生在插入 A 之前。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
顺序可能会以某种方式交换。
我建议您首先在
表 A
中执行所有插入
,然后才在表 B
中执行插入
。另外,您发布的插入内容似乎不正确。
有关示例,请参阅此处。
The order might be swapped somehow.
I recomend you to first do all
inserts
intable A
and only after that doinserts
ontable B
.Also, it seems that the inserts that you posted are incorrect.
See here for examples.
谢谢大家的帮助。
这是我的服务中事务清理过程中错误的副作用。它回滚任何很长很长的事务。当插入表 A 和表 B 之间发生清理时,会抛出错误,因为 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.