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:
AlphabeticalListOfProduct product = (from x in scope.Extent<AlphabeticalListOfProduct>()
where x.ProductID == 1
string queryStr = "SELECT * FROM AlphabeticalListOfProductExtent as a WHERE a.ProductID == 1";
AlphabeticalListOfProduct product =
Dim product As AlphabeticalListOfProduct = (From x In scope.Extent(Of AlphabeticalListOfProduct)() _
Where x.ProductID = 1 _
Dim queryStr As String = "SELECT * FROM AlphabeticalListOfProductExtent as a WHERE a.ProductID == 1"
Dim product As AlphabeticalListOfProduct =
The approach for updating data is the same as for regular tables. Even the referenced object can be changed:
product.ProductName = "Cheese";
product.CategoryID = 4;
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.|