LINQ to SQL - 检索对象、修改、SubmitChanges() 创建新对象

发布于 2024-08-11 19:22:26 字数 538 浏览 4 评论 0原文

我已经为此奋斗了一段时间。我正在尝试实现多对一关联。我的表中有一堆行,称为读数。这些随着时间的推移而积累,我时不时地想将它们导出。当我导出它们时,我想创建一个名为 ExportEvent 的新对象,以跟踪导出了哪些行,以便在需要时可以重新导出它们。因此,Reading 与 ExportEvent 具有可为空的外键关系,因为我在导出读数之前创建了读数。

我发现,当我执行导出时,无论我首先创建 ExportEvent (evt) 并使用添加读数,

evt.Readings.AddRange(), 

还是使用

foreach(reading) 
reading.ExportEvent = evt

当我调用 SubmitChanges 时,我总是会收到一堆与关联创建的新读数evt,原始记录未更新。

不过,我将其简化为最简单的,只是为了看看是否可以创建两个没有关联的对象,我什至发现当我刚刚检索所有读数并更新它们的 int 值时,submitchanges 仍然插入了一堆新记录。这是怎么回事?

I've been battling this for a while. I'm trying to implement a many to one association. I have a bunch of rows in a table, called readings. These accumulate over time, and every now and then I want to export them. When I export them I want to create a new object called ExportEvent, to track which rows were exported, so they can be re-exported if need be. Therefore Reading has a nullable foreign key relationship with ExportEvent, as I create the readings before I export them.

What I'm finding is that when I then do the export, whether I first create the ExportEvent (evt) and add the readings using

evt.Readings.AddRange(), 

or if I use

foreach(reading) 
reading.ExportEvent = evt

When I call SubmitChanges I am always getting a new bunch of readings created with the association to evt, and the original records aren't updated.

I pared this back to its simplest though, just to see if I could create the two objects with no association, and I even found when I just retrieved all the readings and updated an int value on them, submitchanges still inserted a bunch of new records. What's going on?

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

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

发布评论

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

评论(2

过期以后 2024-08-18 19:22:26

嗯。有趣 - 刚刚点击我书签中的这个链接,发现问题又复活了,所以会提供(尴尬的)解决方案。我的所有实体都具有审核数据属性 - CreatedDate 和 UpdatedDate。因此,我实现了数据上下文中每个实体的插入和更新的部分方法。我为新创建的实体复制并粘贴了其中一些插入和更新方法(这经常是导致某些失败的原因)。结果,我还复制了一个错误,其中 Update[blah] 方法调用 ExecuteDynamicInsert,而不是 ExecuteDynamicUpdate。

可以说,当我花了 3 个小时疯狂地尝试解决这个问题时,我感到非常沮丧,却发现这是由于(愚蠢的)复制/粘贴错误造成的 - 并且在我大约 3 分钟后才发现错误。 d 发布了这个问题!

希望这对某人有帮助。

Hmmm. Interesting - just clicked this link in my bookmarks, and found that the question has been resurrected, so will provide the (embarrassing) solution. All of my entities have audit data properties on them - CreatedDate and UpdatedDate. Therefore I've implemented the partial methods for the insert and update of each entity in the datacontext. I had copied and pasted (how often is this the cause of some downfall) some of these insert and update methods for the newly created entities. As a result I'd also copied an error, where the Update[blah] methods were calling ExecuteDynamicInsert, instead of ExecuteDynamicUpdate.

Suffice to say I was very frustrated when for 3 hours I'd been trying frantically to solve this problem, only to find it was due to a (silly) copy/paste error - and only to find the error about 3 mins after I'd posted this question!

Hope this helps someone.

秋意浓 2024-08-18 19:22:26

我怀疑这是因为您正在调用 AddRange()。这会将新对象添加到数据上下文中。相反,您应该尝试通过在数据上下文中调用 Attach() 来重新附加现有对象。

(或者,如果您从未分离它们并且仍然拥有原始数据上下文,则无需执行任何操作,只需对对象进行更改并调用 SubmitChanges()

I suspect it is because you are calling AddRange(). This will add the new objects to the data context. Instead, you should try just re attaching the existing objects by called Attach() on your data context.

(Or if you never detached them and still have your original data context, you don't need to do anything, just make the changes to the objects and call SubmitChanges())

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