索引属性错误:必须符合条件且明确提供参数

时间:2012-03-23 14:32:53

标签: asp.net vb.net entity-framework entity-framework-4

在运行下面包含的代码时,我收到错误

  

“EntityCommandExecutionException未被用户代码处理。

然后我被告知要查看内部异常以获取详细信息......我在Data:

下看到了
  

“为了评估索引属性,必须限定属性,并且必须由用户显式提供参数。”

在内部例外情况下 - >消息:

  

“从服务器接收结果时发生传输级别错误。(提供程序:TCP提供程序,错误:0 - 句柄无效。)”

Visual Studio认为错误的代码是:

Dim qpeople = (From p In dbContext2.PEOPLE _
                                    Where p.PEOPLE_ID = ID _
                                    Order By p.CREATE_DATE Descending _
                                    Select p).FirstOrDefault

更大的代码上下文是:

 Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    Dim semester As String = ddlwhSemester.SelectedValue
    Dim year As String = txtwhYear.Text
    Dim exists As String = "N"
    Dim pcsemester As String = ddlSemester.SelectedItem.Text
    Dim pcyear As String = ddlYear.SelectedItem.Text
    Using dbContext As pbu_housingEntities = New pbu_housingEntities
        ' Get the list of residents in webHousing.
        Dim qresidents = (From p In dbContext.Residents _
                          Where p.semester = semester _
                          Where p.year = year _
                         Select p.people_code_id)
        Using dbContext2 As Campus6Entities = New Campus6Entities
            ' Get the list of students in PowerCampus.
            Dim qstudents = (From p In dbContext2.RESIDENCies _
                             Join a In dbContext2.ACADEMICs _
                             On a.PEOPLE_CODE_ID Equals p.PEOPLE_CODE_ID _
                             Where p.ACADEMIC_TERM = pcsemester _
                             Where p.ACADEMIC_YEAR = pcyear _
                             Where a.ACADEMIC_TERM = pcsemester _
                             Where a.ACADEMIC_YEAR = pcyear _
                             Where a.PROGRAM = "UND" _
                             Where (a.CLASS_LEVEL = "FR" _
                             Or a.CLASS_LEVEL = "FRNR" _
                             Or a.CLASS_LEVEL = "FRST" _
                             Or a.CLASS_LEVEL = "SO" _
                             Or a.CLASS_LEVEL = "JR" _
                             Or a.CLASS_LEVEL = "SR" _
                             Or a.CLASS_LEVEL = "SR5" _
                             Or a.CLASS_LEVEL = "Tran") _
                             Select p.PEOPLE_ID).Distinct
            For Each row In qstudents
                exists = "N"
                For Each res In qresidents
                    If row.ToString = res.ToString Then
                        exists = "Y"
                    End If
                Next
                If exists = "Y" Then
                    ' Skip adding.
                Else
                    ' Add a row.
                    ' Get the ID
                    Dim ID As String = row
                    ' Get info from PowerCampus
                    Dim qpeople = (From p In dbContext2.PEOPLE _
                                    Where p.PEOPLE_ID = ID _
                                    Order By p.CREATE_DATE Descending _
                                    Select p).FirstOrDefault
                    Dim people_code_id As String = qpeople.PEOPLE_CODE_ID
                    Dim qacademic = (From p In dbContext2.ACADEMICs _
                                     Where p.PEOPLE_CODE_ID = people_code_id _
                                     Where p.ACADEMIC_TERM = pcsemester _
                                     Where p.ACADEMIC_YEAR = pcyear _
                                     Order By p.CREATE_DATE Descending _
                                     Select p.CLASS_LEVEL).FirstOrDefault
                    Dim qaddress = (From p In dbContext2.ADDRESSes _
                                    Where p.PEOPLE_ORG_CODE_ID = people_code_id _
                                    Where p.ADDRESS_TYPE = "Perm" _
                                    Order By p.CREATE_DATE Descending _
                                    Select p).FirstOrDefault
                    Dim qdemographics = (From p In dbContext2.DEMOGRAPHICS _
                                         Where p.PEOPLE_CODE_ID = people_code_id _
                                         Order By p.CREATE_DATE Descending _
                                         Select p.GENDER).FirstOrDefault

                    ' Create the new occupant.
                    Dim newres As New Resident
                    newres.people_code_id = ID
                    newres.person_name = qpeople.FIRST_NAME + " " + qpeople.MIDDLE_NAME + " " + qpeople.LAST_NAME
                    newres.first_name = qpeople.FIRST_NAME
                    newres.last_name = qpeople.LAST_NAME
                    newres.class_level = qacademic
                    newres.gender = qdemographics
                    newres.semester = semester
                    newres.year = year
                    newres.email = qaddress.EMAIL_ADDRESS
                    newres.create_date = Date.Now
                    dbContext.Residents.AddObject(newres)
                    dbContext.SaveChanges()
                End If
            Next
        End Using
    End Using
End Sub

2 个答案:

答案 0 :(得分:1)

检查您的数据库并确保主键存在。我有一个类似的问题,发现主键没有定义。只是一个想法,可能不是问题,但值得快速检查。

答案 1 :(得分:1)

我之前没有注意到这一点,但您正在设置您的ID:

Dim ID As String = row 

尝试在linq查询之前将ID转换为Int。