对象的键与ObjectContext中的对应属性不匹配

时间:2012-03-23 13:02:57

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

当我执行这个子程序时,它会抛出一个错误,

  

“作为对象键的一部分的属性的值与存储在ObjectContext中的相应属性值不匹配。如果属于键的属性返回不一致或不正确的值或者未调用DetectChanges,则会发生这种情况。在对属于密钥的属性进行更改之后。“

以下是代码,下面我简要解释了代码的作用。

  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 _
                             Where p.ACADEMIC_TERM = pcsemester _
                             Where p.ACADEMIC_YEAR = pcyear _
                             Select p)
            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.PEOPLE_ID
                    ' Get info from PowerCampus
                    Dim qstudent = (From p In dbContext2.PEOPLE _
                                    Where p.PEOPLE_ID = ID _
                                    Order By p.CREATE_DATE Descending _
                                    Select p).FirstOrDefault
                    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).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).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.CLASS_LEVEL
                    newres.gender = qdemographics.GENDER
                    newres.semester = semester
                    newres.year = year
                    newres.email = qaddress.EMAIL_ADDRESS
                    dbContext.Residents.AddObject(newres)
                    dbContext.SaveChanges()
                End If
            Next
        End Using
    End Using
End Sub

以上代码用于将记录从SIS(本质上是高级CRM)提取到我的webHousing应用程序(用于校园居民)。它会获得每个选定学期/年的学生的列表,然后将它们输入到webHousing数据库中(如果它们在该学期/年中尚不存在)。

1 个答案:

答案 0 :(得分:1)

看起来你的qresidents是整数的枚举,而你的qstudents是居民类型的对象的枚举。所以这一行

 If row.ToString = res.ToString Then
                    exists = "Y"
 End If

无法比较类型。您的第二个查询应该是:

 Dim qstudents = (From p In dbContext2.RESIDENCies _
                          Where p.ACADEMIC_TERM = pcsemester _
                         Where p.ACADEMIC_YEAR = pcyear _
                         Select p.people_code_id)