通过 System.Data.Linq.DataContext 插入 SQL Server 时出错

发布于 2024-12-22 02:07:08 字数 547 浏览 1 评论 0原文

我通过事务中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒的傷心 2024-12-29 02:07:08

顺序可能会以某种方式交换。

我建议您首先在表 A 中执行所有插入,然后才在表 B 中执行插入


另外,您发布的插入内容似乎不正确。

有关示例,请参阅此处

The order might be swapped somehow.

I recomend you to first do all inserts in table A and only after that do inserts on table B.


Also, it seems that the inserts that you posted are incorrect.

See here for examples.

友谊不毕业 2024-12-29 02:07:08

谢谢大家的帮助。

这是我的服务中事务清理过程中错误的副作用。它回滚任何很长很长的事务。当插入表 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文