有没有一种方法可以在不使用两个上下文的情况下使用实体框架来完成此任务?
我有以下代码:
Private Sub btnReserve_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReserve.Click
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 the student know.
lblResult.Text = "Sorry, this room is now fully occupied. Please choose another room."
Else
' Get the current year.
Dim getYear = From p In dbContext.Configs _
Where p.Description = "year" _
Select p.textValue
' 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
Occupant.year = getYear.First.ToString
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 Is Nothing Or p.occupant = 0) _
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.id).FirstOrDefault
UpdateBed.First.occupant = GetID
dbContext.SaveChanges()
lblResult.Text = "Success! You have successfully requested residency in this room!"
btnBack.Visible = False
btnReserve.Visible = False
btnHome.Visible = True
End If
End Using
End Sub
问题是,
UpdateBed.First.occupant = GetID
执行时它总是得到 0,因为该行尚未在数据库中创建。我可以将最后一段代码移到它自己的上下文中,但是有更好的方法吗?
I have the following code:
Private Sub btnReserve_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReserve.Click
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 the student know.
lblResult.Text = "Sorry, this room is now fully occupied. Please choose another room."
Else
' Get the current year.
Dim getYear = From p In dbContext.Configs _
Where p.Description = "year" _
Select p.textValue
' 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
Occupant.year = getYear.First.ToString
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 Is Nothing Or p.occupant = 0) _
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.id).FirstOrDefault
UpdateBed.First.occupant = GetID
dbContext.SaveChanges()
lblResult.Text = "Success! You have successfully requested residency in this room!"
btnBack.Visible = False
btnReserve.Visible = False
btnHome.Visible = True
End If
End Using
End Sub
Problem is, that when
UpdateBed.First.occupant = GetID
executes it always get 0, because the row has not yet been created in the database. I can just move this last bit of code out into its own Context, but is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需在该行之前调用
SaveChanges
即可。Just call
SaveChanges
before the line and it will work.您在 UpdateBed.first 上缺少占用者的导航属性,该属性允许您像这样设置占用者:
You're missing a navigational property on UpdateBed.first for occupant that allows you to set the occupant like this: