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

How to: Customize Collection Generation (C#)

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 topic applies to C#. In order to modify collection generation in VB.NET, please refer to How to: Customize Collection Generation (VB).

By default, when you create a new domain model all collections are generated in the following manner:

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

However, in data-binding scenarios you may need to use other types of collections.

This example demonstrates how to modify the default code generation templates so that the navigation properties are of type TrackedList<T> or TrackedBindingList<T> instead of List<T>.

TrackedList<T> and TrackedBindingList<T> can be found in the Telerik.OpenAccess namespace and are used by Telerik Data Access to monitor the changes in the content of the navigation properties.

The process includes the following steps:

  1. Copy the Templates Locally
  2. Preparation of the templates

1. Copy the Templates Locally

  • At the Code Generation Settings screen of either Add Model wizard when creating a new model or Model Settings dialog when editing an existing one, select the Custom Defined radio button and click the Copy Default To... button.

The Copy Default To... button is part of the code generation improvements of Telerik Data Access and is available only in Visual Studio 2010 and Visual Studio 2012. Visual Studio 2008 uses the previous version of the code generation process. This article demonstrates the usage of the newer version of the process only.

  • The Select Folder dialog displays the directory structure of your project. Select an already existing folder, or create a new one to place the code generation templates in and click OK. The path to the entry template, DefaultTemplateCS.tt, will be automatically selected in the path text box. The templates will be copied to the selected directory.

  • Click Finish if you are using the wizard or OK for the Model Settings dialog.

2. Custom changes in the templates

At this stage you are ready to perform the necessary custom changes in the templates:

  • Open PropertiesGenerator.ttinclude
  • Place the following code at the end of the GenerateFieldForProperty() method:

    if (property.IsNavigationProperty && property.IsIEnumerable)
    {
        propertyType = "TrackedList<" + property.Type + ">";
        initialValue = " = new " + propertyType + "()";
    }
    
  • Place the following code at the end of the GenerateClassPropertySignature() method:

    if (property.IsNavigationProperty && property.IsIEnumerable)
    {
        propertyType = "TrackedList<" + property.Type + ">";
    }
    

For TrackedBindingList<T> you only need to changed propertyType in the above code snippets.

The outcome should be similar to this one:

public partial class Category
{
   private TrackedList<Car> _cars = new TrackedList<Car>();
   public virtual TrackedList<Car> Cars
   {
       get
       {
           return this._cars;
       }
   }
}

In the cases when your project is tracked by a source control system, we recommend that the whole OpenAccessTemplates folder is checked-in with it. That allows the entire team to work with the hosting project without any risk for the changes in the templates or in the generated service files to be rewritten.