Data Access has been discontinued. Please refer to this page for more information.

How to: Make Entities Serializable

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

This article will show you how to generate serializable entities by implementing the ISerializable interface

You can enable Serialization for your persistent classes when you are creating your model via the New Domain Model or New Fluent Model wizards. Alternatively, using the Model Settings dialog, you can enable serialization for an already created Domain Model. Here is the process you would need to go through in order to make your entities serializable:

  1. Navigate to the Code Generation Settings screen of the New Domain Model or New Fluent Model wizard. If you are working with an already created model, open the Model Settings dialog and navigate to the Code Generation tab.
  2. Check the Implement ISerializable checkbox. Screen1
  3. You can specify whether to serialize Reference and Collection Navigation Properties using the Ignore Reference Associations and Ignore Collection Associations checkboxes respectively. By default those checkboxes are checked meaning that Reference and Collection navigation properties will not be serialized.
  4. Click Finish to complete the wizard or OK if you are using the Model Settings dialog.
  5. If you are using the Model Settings dialog save the model in order to regenerate it according to the updated code generation settings.

The persistent classes will be generated implementing the ISerializable interface and will be decorated with the Serializable attribute. Additionally you can customize the serialization and deserialization behavior for your entities by implementing the CustomizeSerializationProcess and CustomizeDeserializationProcess partial methods. The following is an example of a serializable entity:

[System.Serializable()]
public partial class Category : System.Runtime.Serialization.ISerializable
{
    private int _categoryID;
    public virtual int CategoryID
    {
        get
        {
            return this._categoryID;
        }
        set
        {
            this._categoryID = value;
        }
    }

    private string _categoryName;
    public virtual string CategoryName
    {
        get
        {
            return this._categoryName;
        }
        set
        {
            this._categoryName = value;
        }
    }

    private string _imageFileName;
    public virtual string ImageFileName
    {
        get
        {
            return this._imageFileName;
        }
        set
        {
            this._imageFileName = value;
        }
    }

    private IList<RentalRate> _rentalRates = new List<RentalRate>();
    public virtual IList<RentalRate> RentalRates
    {
        get
        {
            return this._rentalRates;
        }
    }

    private IList<Car> _cars = new List<Car>();
    public virtual IList<Car> Cars
    {
        get
        {
            return this._cars;
        }
    }

    #region ISerializable Implementation

    public Category()
    {
    }

    protected Category(System.Runtime.Serialization.SerializationInfo info,
        System.Runtime.Serialization.StreamingContext context)
    {
        this.CategoryID = info.GetInt32("CategoryID");
        this.CategoryName = info.GetString("CategoryName");
        this.ImageFileName = info.GetString("ImageFileName");
        CustomizeDeserializationProcess();
    }

    public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info, 
        System.Runtime.Serialization.StreamingContext context)
    {
        info.AddValue("CategoryID", this.CategoryID, typeof(int));
        info.AddValue("CategoryName", this.CategoryName, typeof(string));
        info.AddValue("ImageFileName", this.ImageFileName, typeof(string));
        CustomizeSerializationProcess();
    }

    partial void CustomizeSerializationProcess(System.Runtime.Serialization.SerializationInfo info, 
        System.Runtime.Serialization.StreamingContext context);
    partial void CustomizeDeserializationProcess(System.Runtime.Serialization.SerializationInfo info, 
        System.Runtime.Serialization.StreamingContext context);
    #endregion
}
<System.Serializable()> _
Public Partial Class Category
    Implements System.Runtime.Serialization.ISerializable
    Private _categoryID As Integer 
    Public Overridable Property CategoryID As Integer
        Get
            Return Me._categoryID
        End Get
        Set(ByVal value As Integer)
            Me._categoryID = value
        End Set
    End Property

    Private _categoryName As String 
    Public Overridable Property CategoryName As String
        Get
            Return Me._categoryName
        End Get
        Set(ByVal value As String)
            Me._categoryName = value
        End Set
    End Property

    Private _imageFileName As String 
    Public Overridable Property ImageFileName As String
        Get
            Return Me._imageFileName
        End Get
        Set(ByVal value As String)
            Me._imageFileName = value
        End Set
    End Property

    Private _rentalRates As IList(Of RentalRate)  = new List(Of RentalRate)
    Public Overridable ReadOnly Property RentalRates As IList(Of RentalRate)
        Get
            Return Me._rentalRates
        End Get
    End Property

    Private _cars As IList(Of Car)  = new List(Of Car)
    Public Overridable ReadOnly Property Cars As IList(Of Car)
        Get
            Return Me._cars
        End Get
    End Property

    #Region "ISerializable Implementation"

    Public Sub New()
    End Sub

    Protected Sub New(info As System.Runtime.Serialization.SerializationInfo, _
        context As System.Runtime.Serialization.StreamingContext)
        Me.CategoryID = info.GetInt32("CategoryID")
        Me.CategoryName = info.GetString("CategoryName")
        Me.ImageFileName = info.GetString("ImageFileName")
        CustomizeDeserializationProcess()
    End Sub

    Public Overridable Sub GetObjectData(info As System.Runtime.Serialization.SerializationInfo, _ 
        context As System.Runtime.Serialization.StreamingContext) _
        Implements System.Runtime.Serialization.ISerializable.GetObjectData
        info.AddValue("CategoryID", Me.CategoryID, GetType(Integer))
        info.AddValue("CategoryName", Me.CategoryName, GetType(String))
        info.AddValue("ImageFileName", Me.ImageFileName, GetType(String))
        CustomizeSerializationProcess()
    End Sub

    Private Partial Sub CustomizeSerializationProcess(info As System.Runtime.Serialization.SerializationInfo, _
        context As System.Runtime.Serialization.StreamingContext)
    End Sub
    Private Partial Sub CustomizeDeserializationProcess(info As System.Runtime.Serialization.SerializationInfo, _
        context As System.Runtime.Serialization.StreamingContext)
    End Sub
    #End Region
End Class