Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Use the Reverse Mapping Wizard
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Defining a Data Model > How to: Use the Reverse Mapping Wizard

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.

This topic is describes how to use the Reverse Mapping Wizard in a task-oriented approach.

The Reverse Mapping Wizard reads a schema from the database and helps you to map tables from this schema to classes. You can generate the source code in either VB or C#.

 

During the task the following operations will be demonstrated:

  • Map a table to a class
  • Map a column to a field
  • Ignore certain tables by not mapping them to classes
  • Creating a 1-n collection
  • Map a table to a ‘join table’ that holds a collection
  • Changing the namespace for a class as well as for multiple classes collectively
  • Generate the source code for classes

 

Prerequisites:

This walkthrough uses the Northwind database on SQL Server 2005, to demonstrate the above-mentioned operations. To complete this walkthrough you will need access to the Northwind database on any of the OpenAccess supported database servers.

Preparing the project to use OpenAccess

  1. On the File menu, point to New and then click Project to open the New Project dialog box.
  2. From the list of Visual C# projects, select the Class Library project template, type NorthwindMapping in the Name box and then click OK.
    The project name, NorthwindMapping, is also assigned to the root namespace by default. The root namespace is used to qualify the names of classes in the assembly. This means that all classes generated by the Reverse Engineering wizard will use this namespace by default. You can change the namespace for individual or all the classes using the wizard.

  3. In the Solution Explorer, right-click the NorthwindMapping project node, then choose OpenAccess –> Enable Project from the shortcut menu.
    Alternatively you can select the project node in Solution Explorer and choose Enable Project to use ORM from the OpenAccess menu.
  4. Complete the Enable Project wizard by specifying Northwind as the database and appropriate Server Name. Accept the default values for rest of the options.

Reading the Schema and adjusting the table and field mapping

The Reverse Engineering wizard uses the information specified in the Enable Project wizard to retrieve schema information.

  • Select the NorthwindModel project node in the Solution Explorer. On the OpenAccess menu, click Reverse Mapping(Tables To Classes).
    OpenAccess will connect to the Northwind database and fetch all schema information. It will map all the tables in the schema to classes and all the columns to fields. In most cases, the default values used are appropriate. The wizard now displays all the mapping information in 2 tabs, the Simple View tab and the Advanced View tab. The default settings can be modified using these tabs.

 

  • Map a column to a field
    Click on the Advanced View tab to view a list of classes that are mapped. One of the tables that will be mapped will be the Orders table, which has been mapped to the Order class. Expand the Order tree node and select the ShipName child node. This node represents the ShipName column in the Orders table. By default, the ShipName column will be mapped to a field named shipName. If you want to change the field name to say nameOfShip, type nameOfShip in the Field Name textbox. This changes the name of the generated field to nameOfShip.

  • Change the mapping for a value field
    Click on the Freight tree node (within the Order node) and select float as the data type for the freight field.

  • Ignore certain tables by not mapping them to classes
    In the Simple View select the Region row. Uncheck the check-box in the "Generate" column. This option ignores a table and hence does not generate the corresponding class.

  • Creating a 1:n collection
    1: n collections can be managed using a ‘join’ table or by maintaining a backward reference on the ‘many’ side of the collection.
  1.  Using a BackReference

              Expand the class node that the reference will represent. For e.g. if you need to create a 1:n collection of orderDetails in the Order class, expand the "Order Details" node and select the "order" child-node. Then check the Create one-to-many list check box and type the desired name of the collection in the Inverse Field Name textbox(or use the default value).This will create an orderDetails collection reference in the Order class.

  2. Using a Join Table

                 Select the table that is to be treated as a ‘join table’. For e.g., In the Northwind database, the EmployeeTerritories is a join table. In the "Simple View" tab you can notice that "EmployeeTerritories" is mapped as a collection unlike other tables that ar mapped as classes. That means that a one-to-many collection of type "Territory"  to the Employee class will be generated.

  • Creating a m:n collection

In the "Advanced View" tab select the EmployeeTerritories tree node and check the Many-to-many check box. That will create a m:n relation between the Employee and Territory classes. That means that besides the Employee having a collection of the related "Territory" objects, but a collection of type "Employee" will also be generated as part of the Territory class.

  • Changing the namespace for a class

In the "Simple View" tab select the rows for the classes you want to change the namespace for. Then just type the name of the new namespace in the Change Namespace For The Selected Classes textbox.

 

Generating the source code

When finished with defining the various settings in the Reverse Mapping Wizard just click "Generate & Save Config" and OpenAccess will create the appropriate classes and fields and also set all the required configuration settings in the App.config and reversemapping.config files.

Note that by default OpenAccess generates the persistent classes as partial, divided into two files each. One is named ClassName.Telerik.OpenAccess.cs where the private fields are stored and the other ClassName.cs where properties for the fields in the other partial class are stored. If the classes are regenerated at some point, the ClassName.cs file will not be overwritten in order to preserve the user changes that might have been done. The new content of this file can be found in a commented block at the bottom of the ClassName.Telerik.OpenAccess.cs file and can be manually copied to the first file if necessary.