Serializing LINQ Types

So I am using the new Data Designer in VS 2008 to create my LINQ to SQL DataContext and associated data classes and one of the first thing I wanted to do was to send back one of these “data classes” to an AJAX client via a AJAX-enabled web service. I write the code to return a SalesTerritory class “yes I am using the infamous AdventureWorks database” and hook up all of the associated code and try to run the thing. To my surprise things didn’t work. I open up Nikhil Kothari’s Web Development Helper to find out I have a serialization error.

“Type ‘RIASilverlightWeb.Data.SalesTerritory’ cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.

Well this really bummed me out. I sure the heck didn’t want to decorate all of that generated code with DataContact/DataMember stuff. To my surprise MS has made this easy. All you need to do is go to the properties of the designer and set the SerializationMode to Unidirectional (see below).

SerializationMode_2

What this does is decorate all of your classes with the DataContract attribute and all of your properties with the DataMember attribute. Once you do this the data can be serialized.

This can also be done with SQLMetal using the /serialization:Unidirectional command flag.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>