1

我有一个 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 
4

1 回答 1

0

我怀疑从您的服务生成的代理类不包括 New 关键字,只是设置了属性类型。您应该能够通过显式实例化调用代码来解决此问题:

Dim con As New ef.Contact()

con.ContactTypes_ContactTypeId = co.ContactTypes_ContactTypeId 
con.Value = co.Value 
con.PrivatePs = New PrivatePs()
con.PrivatePs.Add(pp) 
context.Add(con) 

如果您使用的是实体框架,您可能需要检查它们为导航属性生成的修复代码,以确保它们可以正确同步。

于 2012-09-13T17:27:17.880 回答