Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Serialize and Deserialize Objects
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > How to: Serialize and Deserialize Objects

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

Telerik OpenAccess Domain Model types generated by the OpenAccess Create Model Wizard support binary serialization. When you serialize an object to a binary stream, all related objects currently loaded into the OpenAccessContext will also be serialized.

The examples in this topic are based on the Northwind domain model. To run the code in this example, you must have already added the Northwind domain model to your project.

You must also add using statements for the following namespaces:

  • System.IO
  • System.Runtime.Serialization.Formatters.Binary

In this example, a SerializeToBinaryStream method queries for the Customer object for the specified CustomerID value, and returns a binary MemoryStream. The MemoryStream contains an object graph of the Customer object and its related CustomerDemographic and Order objects.

All classes that are part of the object graph which will be serialized should be marked with the [Serializable] attribute. In this example, the Customer, CustomerDemographic and Order classes should be marked with the [Serializable] attribute.
C# Copy Code
private static void SerializeDeserialize()
   BinaryFormatter formatter =
new BinaryFormatter();

   MemoryStream stream = SerializeToBinaryStream(
"ALFKI" );
   stream.Seek( 0, SeekOrigin.Begin );

   Customer customer = formatter.Deserialize( stream )
as Customer;

"Country: {0}", customer.Country );

private static MemoryStream SerializeToBinaryStream( string customerId )
   BinaryFormatter formatter =
new BinaryFormatter();

   MemoryStream stream =
new MemoryStream();
using ( NorthwindDbContext dbContext = new NorthwindDbContext() )
       Customer customer = dbContext.Customers.Where( c => c.CustomerID == customerId ).First();
       formatter.Serialize( stream, customer );

return stream;