针对多个对象的 Linq to SQL InsertOnSubmit

发布于 2024-09-18 01:52:18 字数 821 浏览 2 评论 0原文

我在使用 Linq to SQL InsertOnSubmit 时遇到问题,它似乎只适用于表中的第一项。

例如,以下内容:

var noteDetail1 = new NoteDetail() { Title = "Test Note Title 1", NoteText = "Test note" };
var waiverDetail1 = new WaiverDetail() { Title = "Test Waiver Title 1", NoteText = "Test waiver details text" };
var riskDetail1 = new RiskDetail() { Title = "Test Risk Title 1", NoteText = "Test risk details text" };
context.Notes.InsertOnSubmit(noteDetail1);
context.Notes.InsertOnSubmit(riskDetail1);
context.Notes.InsertOnSubmit(waiverDetail1);
context.SubmitChanges();

我只将第一个实体(“测试注释标题 1”)插入到数据库中。如果我在每个 InsertOnSubmit 之后放置 SubmitChanges,则所有行都会成功插入。

上面的Types都是继承自Note类,所以插入到同一个表中。

然而,我在非派生类方面遇到了同样的问题。

我花了很长时间看这个,但找不到我做错了什么。 InsertOnSubmit/SubmitChanges 的整个想法是这样你可以进行多次更改,所以一定有一些简单的东西我丢失了。

I have a problem with Linq to SQL InsertOnSubmit, which only seems to work for the first item in a table.

For example with the following:

var noteDetail1 = new NoteDetail() { Title = "Test Note Title 1", NoteText = "Test note" };
var waiverDetail1 = new WaiverDetail() { Title = "Test Waiver Title 1", NoteText = "Test waiver details text" };
var riskDetail1 = new RiskDetail() { Title = "Test Risk Title 1", NoteText = "Test risk details text" };
context.Notes.InsertOnSubmit(noteDetail1);
context.Notes.InsertOnSubmit(riskDetail1);
context.Notes.InsertOnSubmit(waiverDetail1);
context.SubmitChanges();

I only get the first entity ("Test Note Title 1") inserted into the database. If I place a SubmitChanges after each InsertOnSubmit, all the rows are successfully inserted.

The above Types are all inherited from a Note class, so are inserted into the same table.

I am, however, experiencing the same problem with non-derived classes.

I've spent a long time looking at this but can't find what I've done wrong. The whole idea of InsertOnSubmit/SubmitChanges is so that you can do multiple changes so there must be something simple I am missing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绝對不後悔。 2024-09-25 01:52:25

问题是我在实体类中重写了 Equals,以便具有相同 Id 的实体被认为是相同的。显然,Linq to SQL 在某个时刻使用了这一点并得到所有新实体都相等的结果(因为它们的 Id 都为 0)。

感谢 Jonathan 成为我的“Rubber Duck”

The problem was that I had overriden Equals in my entity classes so that entities with the same Id were considered the same. Obviously, Linq to SQL is using this at some point and getting the result that all new entities are equal (because they all have the Id of 0).

Thanks Jonathan for being my "Rubber Duck".

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