Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Reverse Map and Use Database Views with Telerik OpenAccess ORM
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Defining a Data Model > How to: Reverse Map and Use Database Views with Telerik OpenAccess ORM

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 is a step-by-step guide to the process of reverse mapping database views. They differ from the regular tables and that is why an additional step has to be performed during the mapping.
All examples in the article are based on the Northwind database and a project of type Class library. The project first has to be “Enabled” to use Telerik OpenAccess ORM. Open the OpenAccess menu and run the Enable Project wizard. It will help you to configure the project and the database connection. Once the project is enabled, you are ready to start mapping. The steps below demonstrate mapping the “Alphabetical list of products” view available in the Northwind database. It contains columns from two related tables – Products and Categories.

Step 1: Open the Reverse Mapping wizard


The Simple View tab shows all available tables and views from the database along with some basic options needed to create the code. Each table/view can be mapped to a Class, Collection or Map. Usually collections and maps are used to represent join tables. When mapping views, the Class option should be selected. Custom class name and namespace can be specified here as well.


Step 2: Switch to Advanced View.


The Advanced View tab provides options that are not available in the Simple View but are required in order to map a view. Select the Views node from the Treeview. A grid similar to the one from the previous step shows up. Now you have to choose which views to be mapped. To do this, enable the relevant “Generate” checkbox next to each view. You may notice that the treeview icons have been changed for the views which are marked to be mapped.


Step 3:  This step is specific for mapping views. They do not provide information about identity columns as the regular tables do. However, all persistent classes generated by Telerik OpenAccess ORM require an identity field. This identity field has to be set manually for each class. Expand the view from the list in the treeview and select the id field. Then enable the “Primary Key” checkbox on the right. After setting an identity, the red warning about a missing primary key field should disappear from the code preview window.

Views without primary key columns are not currently supported. The uniqueness of each row is required for write operations.


Step 4: When you have chosen identity fields for all classes, they are ready to be generated. Click the “Generate & Save Config” button.
After the classes are created they can be used to retrieve and modify data. The code samples below demonstrate basic read and update operations on the “Alphabetical list of products” view. Both OQL and Linq queries can be executed to fetch data:

C# Copy Code
AlphabeticalListOfProduct product = (from x in scope.Extent<AlphabeticalListOfProduct>()
                                    where x.ProductID == 1
                                    select x).First();
string queryStr = "SELECT * FROM AlphabeticalListOfProductExtent as a WHERE a.ProductID == 1";
AlphabeticalListOfProduct product =
VB.NET Copy Code
Dim product As AlphabeticalListOfProduct = (From x In scope.Extent(Of AlphabeticalListOfProduct)() _
                                            Where x.ProductID = 1 _
                                            Select x).First()
Dim queryStr As String = "SELECT * FROM AlphabeticalListOfProductExtent as a WHERE a.ProductID == 1"
Dim product As AlphabeticalListOfProduct =
              scope.GetOqlQuery(Of AlphabeticalListOfProduct)(queryStr).ExecuteEnumerable().First()

The approach for updating data is the same as for regular tables. Even the referenced object can be changed:

C# Copy Code
product.ProductName = "Cheese";
product.CategoryID = 4;
VB.NET Copy Code
product.ProductName = "Cheese"
product.CategoryID = 4

A running transaction is required to store the changes to the database.

Currently create and delete operations are not supported for views containing columns from more than one table.