You can serialize or de-serialize an entity instance using the ObjectContext
. To do this, you need to call the Serialize
or the Deserialize
method of the BinaryFormatter
class as shown in the code snippets below:
private void Serialize(String fileName,Object obj) { BinaryFormatter binaryFormatter = new BinaryFormatter(); FileStream fileStream = new FileStream(fileName,FileMode.Create); try { binaryFormatter.Serialize(fileStream, obj); } catch (SerializationException ex) { throw new ApplicationException("The object graph could not be serialized", ex); } finally { fileStream.Close(); } }
The Serialize
method shown above accepts a file name and the object to be serialized as parameters, serializes it using a BinaryFormatter
instance, and then stores the serialized instance of the file.
Here is the DeSerialize
method that accepts the name of the file where the serialized instance is stored and returns the de-serialized instance back:
public Object...