指定的LINQ表达式包含对与不同上下文关联的查询的引用

时间:2011-04-05 20:12:58

标签: asp.net vb.net entity-framework entity-framework-4 linq-to-entities

我知道stackoverflow上有类似的问题 - 我仔细查看了它们并认为我的问题有点类似,但是通过查看其他任何问题/答案都无法找到解决方案。 尝试执行以下代码时出现错误:

Private Sub btnReserve_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReserve.Click
    ' Check that the room is still available.
    Dim dbCheckOccupants As New pbu_housingEntities
    Dim hall As String = CStr(Session("hall"))
    Dim room As String = CStr(Session("room"))
    Dim checkOccupants = From p In dbCheckOccupants.Rooms _
                         Let building_id = p.Building1.id _
                         Where p.building_name = hall _
                         Where p.room1 = room _
                         Select p.current_occupancy, p.max_occupancy, p.id, building_id
    If checkOccupants.First.current_occupancy >= checkOccupants.First.max_occupancy Then
        ' If it isn't available, let student know.
        lblResult.Text = "Sorry, this room is now fully occupied. Please choose another room."
    Else
        ' If it is available, add the student to the room.
        Dim AddOccupant As New pbu_housingEntities
        Dim Occupant As New Resident
        Dim gender As String = CStr(Session("gender"))
        Dim person_name As String = CStr(Session("person_name"))
        Dim class_level As String = CStr(Session("class_level"))
        Dim semester As String = CStr(Session("term"))
        Dim people_code_id As String = CStr(Session("people_code_id"))
        Dim first_name As String = CStr(Session("first_name"))
        Dim last_name As String = CStr(Session("last_name"))
        Dim building_id As String = checkOccupants.First.building_id
        Dim room_id As String = checkOccupants.First.id
        Occupant.building = building_id
        Occupant.room = room_id
        Occupant.gender = gender
        Occupant.person_name = person_name
        Occupant.class_level = class_level
        Occupant.semester = semester
        Occupant.people_code_id = people_code_id
        Occupant.create_date = Date.Now
        Occupant.first_name = first_name
        Occupant.last_name = last_name
        AddOccupant.Residents.AddObject(Occupant)
        AddOccupant.SaveChanges()
        ' Increment the number of occupants in the room.
        Dim UpdateRoomOccupancy As New pbu_housingEntities
        Dim UpdateOccupancy = (From p In UpdateRoomOccupancy.Rooms _
                               Where p.building_name = hall _
                               Where p.room1 = room _
                               Select p).First
        UpdateOccupancy.current_occupancy = UpdateOccupancy.current_occupancy + 1
        UpdateRoomOccupancy.SaveChanges()
        ' Add the student to a bed.
        Dim AddBed As New pbu_housingEntities
        Dim UpdateBed = From p In AddBed.Beds _
                        Where p.building = building_id _
                        Where p.room = room_id _
                        Where p.occupant = "" _
                        Select p

        ' Get the student's ID from the residency table.
        Dim GetID = From p In AddBed.Residents _
                    Where p.people_code_id = people_code_id _
                    Order By p.id Descending _
                    Select p

        Dim myID As String = GetID.First.id.ToString

        UpdateBed.First.occupant = myID
        AddBed.SaveChanges()
        lblResult.Text = "Success! You have successfully requested residency in this room!"
    End If
End Sub

在这一行上发现错误:

Dim myID As String = GetID.First.id.ToString

据我所知,我没有使用多个上下文?

2 个答案:

答案 0 :(得分:2)

我没有确切地知道为什么你可以在你指定的行上得到一个例外但是代码在我看来有两个缺点:你在这个方法中实例化4个不同的上下文(虽然我没有看到你混合那些对象上下文)并且你不会处置它们中的任何一个。 (数据库上下文引用外部资源(数据库连接),这是一个很好的实践。)

尝试重写它,以便只有一个上下文dbContextSaveChanges只被调用一次,这个单个上下文被放置在方法的末尾(通过Using块):

Private Sub btnReserve_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReserve.Click
    ' Check that the room is still available.
    Using (dbContext As pbu_housingEntities = New pbu_housingEntities)
        ' Check that the room is still available.
        Dim hall As String = CStr(Session("hall"))
        Dim room As String = CStr(Session("room"))
        Dim checkOccupants = From p In dbContext.Rooms _
                             Let building_id = p.Building1.id _
                             Where p.building_name = hall _
                             Where p.room1 = room _
                             Select p.current_occupancy, p.max_occupancy, p.id, building_id
        If checkOccupants.First.current_occupancy >= checkOccupants.First.max_occupancy Then
            ' If it isn't available, let student know.
            lblResult.Text = "Sorry, this room is now fully occupied. Please choose another room."
        Else
            ' If it is available, add the student to the room.
            Dim Occupant As New Resident
            Dim gender As String = CStr(Session("gender"))
            Dim person_name As String = CStr(Session("person_name"))
            Dim class_level As String = CStr(Session("class_level"))
            Dim semester As String = CStr(Session("term"))
            Dim people_code_id As String = CStr(Session("people_code_id"))
            Dim first_name As String = CStr(Session("first_name"))
            Dim last_name As String = CStr(Session("last_name"))
            Dim building_id As String = checkOccupants.First.building_id
            Dim room_id As String = checkOccupants.First.id
            Occupant.building = building_id
            Occupant.room = room_id
            Occupant.gender = gender
            Occupant.person_name = person_name
            Occupant.class_level = class_level
            Occupant.semester = semester
            Occupant.people_code_id = people_code_id
            Occupant.create_date = Date.Now
            Occupant.first_name = first_name
            Occupant.last_name = last_name
            dbContext.Residents.AddObject(Occupant)

            ' Increment the number of occupants in the room.
            Dim UpdateOccupancy = (From p In dbContext.Rooms _
                                   Where p.building_name = hall _
                                   Where p.room1 = room _
                                   Select p).First
            UpdateOccupancy.current_occupancy = UpdateOccupancy.current_occupancy + 1

            ' Add the student to a bed.
            Dim UpdateBed = From p In dbContext.Beds _
                            Where p.building = building_id _
                            Where p.room = room_id _
                            Where p.occupant = "" _
                            Select p

            ' Get the student's ID from the residency table.
            Dim GetID = From p In dbContext.Residents _
                        Where p.people_code_id = people_code_id _
                        Order By p.id Descending _
                        Select p

            Dim myID As String = GetID.First.id.ToString

            UpdateBed.First.occupant = myID

            dbContext.SaveChanges()

            lblResult.Text = "Success! You have successfully requested residency in this room!"
        End If
    End Using
End Sub

运气不错,错误消失,代码仍然和以前一样。

修改

我只是快速添加一个通用注释:也许你有(错误的)想法,你需要为每个查询,插入或删除操作一个新的上下文。事实并非如此,实际上是不良的做法,在不同的环境中混合对象的风险很高。在大多数情况下,您可以遵循方法中的标准模式,如下所示:

' Create one single context
Using (dbContext As pbu_housingEntities = New pbu_housingEntities)

    ' Use here this dbContext for as many queries, Adds, Deletes and Changes
    ' as you need and like

    ' Save ALL Adds, Deletes and Changes you have done in this block
    dbContext.SaveChanges()

End Using

知道SaveChanges将执行数据库事务也很重要,因此它是一个All-Or-Nothing操作。我可以想象这在你的方法中也很重要。例如,您使用Increment the number of occupants in the room.Add the student to a bed.评论的两个操作,它们是否应该同时发生,或者不发生任何一个以避免数据库中的不一致状态?使用不同的上下文,第一个操作可能成功但第二个操作失败,使数据在数据库中处于不一致状态。

答案 1 :(得分:1)

你能不能在一次通话中获得身份证?

Dim MyID = (From p In AddBed.Residents _
                    Where p.people_code_id = people_code_id _
                    Order By p.id Descending _
                    Select p.id).SingleOrDefault()

您通过它的外观调用上下文的多个实例。尝试使用另一个答案推荐的使用块