在数据上下文中插入的值在提交更改之前在数据上下文中不可用?

发布于 2024-07-15 01:59:47 字数 918 浏览 9 评论 0原文

我正在浏览一个包含文章和撰写这些文章的记者的 XML 文件。 当我们将文章添加到 _Data 我们的数据上下文中时,我们可能会遇到需要添加的记者,因此我们这样做:

newJourno = New journalist With {.name = strJournalist}
_Data.journalists.InsertOnSubmit(newJourno)
.articles_journalists.Add(New articles_journalist With {.id_journalist = newJourno.id,         .id_article = .id})

但是随后我们可能会再次遇到同一位记者,并且当我们这样做时不会返回任何内容:

Dim journo = _Data.journalists.Where(Function(s) s.name = strJournalist).SingleOrDefault

因此它再次使用上面的代码再次插入同一位记者。

一旦我们所有的插入完成,我们就会进行提交更改。 此时它有一个头部适合:

INSERT 语句与 COLUMN FOREIGN KEY 约束“FK_articles_journalists_journalists”冲突。 冲突发生在数据库“blah”、表“journalists”、列“id”中。 该语句已终止。

通过查看 sql profiler 中生成的 sql,您可以看到它多次尝试添加一些记者,这将失败,因为名称必须不同。 由于记者未更新,随后尝试插入这些记者的记录失败。

当然,如果我有一个记者集合,添加一些记者,然后查看我的集合,我应该看到所有的记者,而不仅仅是原始的记者。 我想我可以通过提交更改来捏造它,但这似乎有点愚蠢。

预先感谢,

戴夫。

I'm going through an XML file of articles and the journalist(s) that wrote them. As we are adding the articles into _Data our datacontext we may come across a journalist that needs adding so we do this:

newJourno = New journalist With {.name = strJournalist}
_Data.journalists.InsertOnSubmit(newJourno)
.articles_journalists.Add(New articles_journalist With {.id_journalist = newJourno.id,         .id_article = .id})

However subsequently we may come across this same journalist again and nothing is returned when we do this:

Dim journo = _Data.journalists.Where(Function(s) s.name = strJournalist).SingleOrDefault

So it uses the code above again to insert the same journalist again.

Once all of our inserts are done we do a submitchanges. At this point it has a head fit:

INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_articles_journalists_journalists'. The conflict occurred in database 'blah', table 'journalists', column 'id'. The statement has been terminated.

From looking through the sql generated in sql profiler you can see that it is trying to add some journalists more than once, this will fail as the name must be distinct. The subsequent records that are trying to be inserted with these journalists are failing as the journalist wasn't updated.

Surely if I have a collection of journalists, add some to it and then look in my collection I should see all of them and not just the original ones. I can fudge it I guess by doing a submitchanges but that seems a bit silly.

Thanks in advance,

Dave.

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

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

发布评论

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

评论(3

丢了幸福的猪 2024-07-22 01:59:47

如果要向数据库添加两个子父行,则必须分配实体,而不是 Id 列,Id 将自动生成,并且仅在提交更改后才可用。

您必须创建一个 articles_journalist 对象,然后将 newJourno 实体分配给它:

articles_journalist.journalist = newJourno;

If you want to add two child-parent rows to the database, you must assign the entity, instead of the Id column, the Id will be autogenerated and will be available only after the submit changes.

You have to do a articles_journalist object, and then assign the newJourno entity to this:

articles_journalist.journalist = newJourno;
哭泣的笑容 2024-07-22 01:59:47

CMS 关于需要分配对象而不是 id 的说法是正确的。

然而,这似乎并没有解决数据上下文在您提交更改之前没有意识到已添加新内容的问题。 我只能假设这是设计使然,因此我现在在代码插入我们稍后搜索的对象时调用submitchanges。

CMS is right about needing to assign the object, not the id.

However this doesn't seem to get around the problem of the datacontext not realising that it has had new stuff added to it until you submitchanges. I can only presume this is by design and therefore I am now calling submitchanges as and when the code inserts objects that we later search for.

枫林﹌晚霞¤ 2024-07-22 01:59:47

“名字必须是独特的。”

这是一个严重的设计缺陷。 人名从来都不是唯一的。

"the name must be distinct."

This is a serious design flaw. Person names are never unique.

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