In my project we serialize disconnected Linq-to-sql entities(mainly to preserve them between postbacks). Code in use for that is fairly straightforward:
public static string Serialize<P>(this P entity)
{
StringWriter writer = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(writer);
DataContractSerializer serializer = new DataContractSerializer(typeof(P));
serializer.WriteObject(xmlWriter, entity);
return writer.ToString();
}
It works fine but after deserialization all EntityRef's children for that object are gone and replaced with just a foreign key value. Looks like this problem is due to the lack of bi-directional serialization. Is there existing work around for this problem?