As the name says, I am having some problems serializing my automatically created POCO objects. But first, some information:
I created my data access level using EF 4.0 en ADO.Net POCO Entity Generator by following this guide: http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template -for-the-entity-framework.aspx .
I now have 2 class libraries, one with an EF model and the second with T4 Auto generated POCO objects.
I am currently working on another project where I want to use DAL class libraries. I have to get some objects and serialize them in XML. At first I tried XmlSerializer, but then I found out that it has problems with circulation links. I fixed this problem with XmlIgnore, but then I had a serialization problem:
Public Overridable Property NwlGroup As ICollection(Of NwlGroup)
Because XmlSerializer does not support interfaces.
Second, I tried a DataContractSerializer with the [DataContract] and [DataMember] attributes in a Poco Class file with an auto-generated entity. This worked, but naturally, I had to clear the changes from the automatically generated file, so I wanted to use the MetaDataType attribute. I created an additional file as follows:
Imports System.Runtime.Serialization
Imports System.ComponentModel.DataAnnotations
<MetadataType(GetType(NewsletterCustomerMetadata))>
Partial Public Class NewsletterCustomer
End Class
<DataContract()
Public Class NewsletterCustomerMetadata
<DataMember(Name:="emailaddress", IsRequired:=True)>
Public Overridable Property Emailaddress As String
<DataMember(Name:="name")>
Public Overridable Property Name As String
<DataMember()>
Public Overridable Property NwlGroup As ICollection(Of NwlGroup)
End Class
Auto generated file:
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated from a template.
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.Runtime.Serialization
Public Class NewsletterCustomer
Public Overridable Property ID As Integer
Public Overridable Property Emailaddress As String
Public Overridable Property Name As String
...
Public Overridable Property NwlGroup As ICollection(Of NwlGroup)
Get
If _nwlGroup Is Nothing Then
Dim newCollection As New FixupCollection(Of NwlGroup)
AddHandler newCollection.CollectionChanged, AddressOf FixupNwlGroup
_nwlGroup = newCollection
End If
Return _nwlGroup
End Get
Set(ByVal value As ICollection(Of NwlGroup))
If _nwlGroup IsNot value Then
Dim previousValue As FixupCollection(Of NwlGroup) = TryCast(_nwlGroup, FixupCollection(Of NwlGroup))
If previousValue IsNot Nothing Then
RemoveHandler previousValue.CollectionChanged, AddressOf FixupNwlGroup
End If
_nwlGroup = value
Dim newValue As FixupCollection(Of NwlGroup) = TryCast(value, FixupCollection(Of NwlGroup))
If newValue IsNot Nothing Then
AddHandler newValue.CollectionChanged, AddressOf FixupNwlGroup
End If
End If
End Set
End Property
Private _nwlGroup As ICollection(Of NwlGroup)
...
End Class
Then I try to serialize it in xml
Dim ctx = New ModelEntities(_connectionString)
ctx.ContextOptions.ProxyCreationEnabled = False
ctx.ContextOptions.LazyLoadingEnabled = False
Dim customers = From c In ctx.NwlCustomer
Select c
Where c.SiID = 99
Dim filename As String = "C:\test.txt"
Dim result As NewsletterCustomer = customers.ToList.FirstOrDefault
Dim writer As New FileStream(filename, FileMode.Create)
Dim ser As New DataContractSerializer(GetType(NewsletterCustomer))
ser.WriteObject(writer, customers.ToList.FirstOrDefault)
writer.Close()
NewsletterCustomer xml /, , , DataContract. DataContract NewsletterCustomerMetadata NewsletterCustomer, root node, , DataContract DataMember.
, DataContractSerializer MetaDataType.
:
- POCO CUSTOM XML?
- [DataContract] [DataMember] POCO?
- Auto Generated POCO XML?