正如標題所說,我有一些問題,我的序列化自動生成的POCO對象。但首先一些背景資料:自動生成的POCO系列化與DataContractSerializer的和MetaDataTypeAttribute問題
我一直在使用EF 4.0連接ADO.Net POCO實體生成這個指南創建了數據訪問層:http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx。
我現在有2個類庫,一個具有EF模型和第二用T4自動生成POCO實體。
目前我正在另一個項目中,我想使用我的DAL類庫。我必須檢索一些對象並將它們序列化爲XML。首先,我嘗試了XmlSerializer,但是後來發現它存在circral引用的問題。我上的使用XmlIgnore這個問題,但後來我有問題序列化:
Public Overridable Property NwlGroup As ICollection(Of NwlGroup)
因爲XmlSerializer的不支持接口。
第二I試圖的DataContractSerializer使用[DataContract]和[數據成員]在自動生成的實體波索類文件屬性。這工作,但自然我不得不清理自動生成的文件的變化,因此我想使用MetaDataType屬性。我創建額外的文件是這樣的:
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>
' 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
#Region "Primitive Properties"
Public Overridable Property ID As Integer
Public Overridable Property Emailaddress As String
Public Overridable Property Name As String
...
#Region "Navigation Properties"
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
然後我試圖序列它爲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,則會按照您會注意的順序排列屬性。如果我從NewsletterCustomerMetadata中將DataContract屬性移動到NewsletterCustomer,那麼我只會在您指定DataContract時沒有DataMember屬性的情況下獲得根節點。
它看起來像的DataContractSerializer不MetaDataType數據註釋工作。
我的問題是:
- 我如何可以序列我的POCO類自定義XML?
- 如何將[DataContract]和[DataMember]屬性添加到自動生成的POCO類中?
- 將自動生成的POCO類序列化爲XML的最佳方式是什麼?
感謝您的回答。我想我會看@Marc提出的某種DTO解決方案。 – MLewi 2011-06-08 11:44:11