-->

Serialize Entity Framework object with children to

2019-01-23 21:21发布

问题:

I'm querying data with parent/child result sets using Entity Framework and I want to export this data to an XML document.

var agreement = storeops.Agreements.SingleOrDefault(a => a.AgreementNumber == AgreementTextBox.Text);
XmlSerializer serializer = new XmlSerializer(agreement.GetType());
XmlWriter writer = XmlWriter.Create("Agreement.xml");
serializer.Serialize(writer, agreement);

This works well except it only serializes the parent without including the related child records in the XML. How can I get the children to serialize as well?

I also tried using POCO generated code and the child collections attempt to be serialized except they are ICollections which can't be serialized.

Cannot serialize member DataSnapshots.Agreement.AgreementItems of type System.Collections.Generic.ICollection`1[[DataSnapshots.AgreementItem, DataSnapshots, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface.

回答1:

XML serialization behaves differently than binary serialization and data contract serialization when working with Entity Framework entities. The latter will serialize any related objects that have been loaded into the object graph, but XML serialization does not, so you will need to use a DataContractSerializer:

var agreement = storeops.Agreements.SingleOrDefault(a => a.AgreementNumber == AgreementTextBox.Text);
// make sure any relations are loaded

using (XmlWriter writer = XmlWriter.Create("Agreement.xml"))
{
    DataContractSerializer serializer = new DataContractSerializer(agreement.GetType());
    serializer.WriteObject(writer, agreement);
}

Also, Entity Framework uses lazy loading by default for 1:Many relations, and if the referenced objects haven't already been loaded when you go to serialize, then all you'll get is the keys that refer to them. You have to explicitly load the related entities either by calling agreement.Children.Load() or by using .Include("Children") in your query (where "Children" is the name of the collection of related entities).



回答2:

I finally figured out a solution to this but it does require editing the generated classes :(

Create POCO generated entity classes, set Lazy Loading to true which will get the parent and all children with one select (without having to use Include or Load).

On the parent class modify the child accessor type from ICollection to FixupCollection.

public virtual FixupCollection AgreementItemLogs

Then in the XmlSerializer you have to specify the parent type and child types from the proxy classes.

var agreement = storeops.Agreements.Include("AgreementItems").SingleOrDefault(a => a.AgreementNumber == AgreementTextBox.Text);                                             
                var typeList = new List<Type>();

                if(agreement.AgreementItems.Count > 0)
                    typeList.Add(agreement.AgreementItems.FirstOrDefault().GetType());
                if (agreement.AgreementItemLogs.Count > 0)
                    typeList.Add(agreement.AgreementItemLogs.FirstOrDefault().GetType());
                if (agreement.AgreementPricings.Count > 0)
                    typeList.Add(agreement.AgreementPricings.FirstOrDefault().GetType());
                if (agreement.AgreementSnapshots.Count > 0)
                    typeList.Add(agreement.AgreementSnapshots.FirstOrDefault().GetType());
                if (agreement.AgreementTransactions.Count > 0)
                    typeList.Add(agreement.AgreementTransactions.FirstOrDefault().GetType());
                if (agreement.AgreementTransactionLogs.Count > 0)
                    typeList.Add(agreement.AgreementTransactionLogs.FirstOrDefault().GetType());

                XmlSerializer serializer = new XmlSerializer(agreement.GetType(), typeList.ToArray());
                XmlWriter writer = XmlWriter.Create("Agreement.xml");
                serializer.Serialize(writer, agreement);