I have a class generated by Linq2Sql:
public partial class BuyerOrder : INotifyPropertyChanging, INotifyPropertyChanged
I want to clone object of this class, like it’s done in this post. For this purpose I define the partial class in not generated file, which I mark as serializable:
[Serializable]
public partial class BuyerOrder
But when I’m calling
formatter.Serialize(stream, source);
I’m getting an exception, saying that this class is not marked as serializable. What am I doing wrong?
If you want to serialize a LINQ-to-SQL type, then tell the code-gen to emit serializable data. You can do this in the DBML, or more simply in the designer – just set the serialization mode to unidirectional (this is the
@Serializationattribute on the root<Database>element in the DBML).This will generate attribute markers suitable for use with
DataContractSerializer; LINQ-to-SQL is designed to be serializable withDataContractSerializer. It is not designed to be serializable withBinaryFormatter.