实体查询未正确更新每个

发布于 2024-10-31 02:21:54 字数 902 浏览 0 评论 0原文

我有以下代码:

  ' Now retabulate the current occupancy.
        For Each row In assignments
            Try
                Dim assignment As Integer = row.room
                Dim student_id As String = row.id
                Dim update_occupancy = (From p In dbContext.Beds _
                                        Where p.occupant = 0 _
                                        Where p.room = assignment _
                                        Select p).FirstOrDefault
                update_occupancy.occupant = student_id

            Catch ex As Exception

            End Try

        Next
        dbContext.SaveChanges()

在这一部分中,我正在迭代当前的作业,当床未被占用(= 0)并且房间等于当前学生时,我将学生插入该房间。

问题是 update_occupancy 似乎总是保持不变。例如,我可能有四个床位(ID 101、102、103、104),第一次学生应该分配到 101,下一个学生分配到 102,下一个学生分配到 103,依此类推。但这只是一遍又一遍地覆盖第一个。我无法将 dbContext.SaveChanges() 放入 For Each 内部,它会损坏。想法?

I have the following code:

  ' Now retabulate the current occupancy.
        For Each row In assignments
            Try
                Dim assignment As Integer = row.room
                Dim student_id As String = row.id
                Dim update_occupancy = (From p In dbContext.Beds _
                                        Where p.occupant = 0 _
                                        Where p.room = assignment _
                                        Select p).FirstOrDefault
                update_occupancy.occupant = student_id

            Catch ex As Exception

            End Try

        Next
        dbContext.SaveChanges()

In this segment I am iterating through the current assignments and when the bed is not occupied (=0) and the room is equal to the current student, I insert the student into that room.

Problem is that update_occupancy seems to always be staying the same. For example, I might have four beds (ID 101, 102, 103, 104), the first time the student should be assigned to 101, the next student to 102, the next to 103, and so on. But it is just overwriting the first one over and over again. I can't put a dbContext.SaveChanges() inside of the For Each, it breaks. Thoughts?

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

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

发布评论

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

评论(1

鯉魚旗 2024-11-07 02:21:54

这是一个明显的问题。您正在向数据库请求一条没有占用者的记录并分配一个新占用者,但如果不保存更改,您将在循环中执行此操作,因此查询必须始终返回同一行。

仅当 assignments 是查询的枚举结果时,才会发生异常。如果您想避免该异常,只需在查询上调用 ToList 即可。

尽管如此,我还是会考虑算法并尝试预先加载床。这需要一次查询来获取分配,并为每张床进行一次查询 + 一次更新。如果您的分配返回 1.000 行,您将需要对床位进行 1.000 次查询。您将无法避免 1.000 更新,因为 EF 不支持命令批处理。算法中还决定什么是交易。是更新单人床还是处理所有加载的分配?基于这个决定,你还可以考虑你的算法。

顺便提一句。如果分配是一个查询,它很可能被重写为一些与床的连接,这样你就不需要在循环中执行子查询。

That is an obvious problem. You are asking your database for a record without occupant and assigning a new occupant but without saving changes you are doing this in loop so the query must always return the same row.

Your exception happens only if assignments is enumerated result of the query. If you want to avoid that exception simply call ToList on the query.

Still I would think about the algorithm and try to load beds upfront. This requires one query to get assignments and one query + one update for each bed. If your assignemnts return 1.000 rows you will have to do 1.000 queries for beds. You will not avoid 1.000 updates because EF doesn't support command batching. There is also decission about what is a transaction in your alogrithm. Is it update of single bed or is it processing of all loaded assignments? Based on this decission you can also think about your algorithm.

Btw. if assignments is a query it can be most probably rewritten to some join with beds so you will not need to do sub queries in loop.

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