我有一个 DataContract 如下:
<DataContract()> _
Public Class PrivateProfile
 ' This member is serialized.
<DataMember()> _
Public UserAccount As UserAccount
<DataMember()> _
Public ProfileName As String = ""
<DataMember()> _
Public Birthday As Date?
<DataMember()> _
Public Gender As Int16 = 0
<DataMember()> _
Public AboutMe As String = ""
<DataMember()> _
Public Locations As New List(Of Location)
<DataMember()> _
Public Contacts As New List(Of Contact)
End Class
联系方式是:
<DataContract()> _
Public Class Contact
' This member is serialized.
<DataMember()> _
Public Value As String
<DataMember()> _
Public ContactTypes_ContactTypeId As Int16
<DataMember()> _
Public PrivatePs As New List(Of PrivateProfile)
End Class
在客户端,我使用以下代码:
Dim svcProfile As New svcProfiles.ProfilesClient
.....
Dim pprofile As New svcProfiles.PrivateProfile
.....
Dim co As New svcProfiles.Contact
        co.Value = "34234234234"
        co.ContactTypes_ContactTypeId = 1
        pprofile.Contacts.Add(co)
一旦我将联系人添加到个人资料(最后一行),我就会收到 NullReferenceException。联系人总是没什么,但我不知道为什么。
任何想法......谷歌搜索了很多......
编辑: 当我在 wcf 实现(服务器)端以相同的情况运行时,我遇到了同样的问题:
If pprofile.Contacts.Count > 0 Then 
    Dim co As New contracts.Contact 
    For Each co In pprofile.Contacts 
        Dim con As New ef.Contact()
        con.ContactTypes_ContactTypeId = co.ContactTypes_ContactTypeId 
        con.Value = co.Value 
        con.PrivatePs.Add(pp) 
        context.Add(con) 
    Next 
    context.SaveChanges() 
End If