【发布时间】:2011-07-30 07:07:47
【问题描述】:
我知道在 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
据我所知,我没有使用多个上下文?
【问题讨论】:
标签: asp.net vb.net entity-framework entity-framework-4 linq-to-entities