+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Forward Mapping, Open Access and Sitefinity

by Georgi Chokov

In a future release, Telerik's new OpenAccess ORM, which was recently acquired from Vanatec, will replace Nolics as Sitefinity's official ORM. One of Nolics advantages over many ORMs was forward mapping, which allowed the developer to make database tables from classes. This feature is also supported by OpenAccess and the process involves creating a class with the persistent attribute, adding public properties within the class, and creating a web application with a data bound tool.

  

To use forward mapping, you must enable your project to use OpenAccess by following these steps:

 

1. Create a new class library project and give it a name

 

2. In the solution explorer, click on the class library that you have created. On the top menu, click on Open Access > Enable Project to use ORM as shown below this sentence:

 

Open Access Menu 

 

3. On page 1 and 2 of the wizard, click next

 

4. On page 3, complete your connection information. The first textbox is just a connection same, so it can be anything you like. The last textbox is the actual name of the database. Here is an example of what your screen could look like:

 

 Enable Project Wizard

 

5. Click next and on the last page, click finish

 

6. Open the solution explorer and click on the class library

 

7. Press F4 and a properties menu will show. Change the update database property to true as shown below this sentence:

 

This will tell OpenAccess to update your database schema, based on the class, during each project build. Now, you must create a class to be mapped to the database by following these instructions:

 

8. In your class library, delete the Class1.cs file

 

9. Add a new class called ProductTable.cs

 

10. In the using section, add this line of code:

using Telerik.OpenAccess;

 

11. Add the following line of code above the ProductTable class:

[Telerik.OpenAccess.Persistent]

public class ProductTable

12. This line of code will tell OpenAccess to create a table in the database called ProductTable. For more info on persistence, please read this article.

13. Inside the class, add public properties for each column name you would like.

Create a public property for your identity column and use the forward mapping wizard to establish single identity as shown below this sentence:

For more info on single identity, please refer to this article. Your code should now look like this:

namespace Forward12122008 
    [Telerik.OpenAccess.Persistent(IdentityField = "_productIdentity")] 
    publicclass ProductTable 
    { 
        //Open Access requires a no-args constructor
        public ProductTable() { } 
        privatestring _productName; 
        privatedecimal _price; 
        privatestring _description; 
        privateint _sku; 
        privateint _productIdentity; 
 
        publicint ProductIdentity 
        { 
            get
            { 
                return _productIdentity; 
            } 
            set
            { 
                _productIdentity = value; 
            } 
        } 
 
 
        publicstring ProductName 
        { 
            get
            { 
                return _productName; 
            } 
            set
            { 
                _productName = value; 
            } 
        } 
 
        publicdecimal Price 
        { 
            get
            { 
                return _price; 
            } 
            set
            { 
                _price = value; 
            } 
        } 
 
        publicstring Description 
        { 
            get
            { 
                return _description; 
            } 
            set
            { 
                _description = value; 
            } 
        } 
 
        publicint SKU 
        { 
            get
            { 
                return _sku; 
            } 
            set
            { 
                _sku = value; 
            } 
        } 
 
    } 
 

14. Build the project by pressing Ctrl + F5.

 

Now that this is set-up, you can set-up a RadGrid, for example, to insert, update, delete or read information. OpenAccess uses a data source control similar to the SQLDataSource and LINQDataSource controls, so in many scenarios you can bind data with limited lines of code. For more info on using the OpenAccessDataSource control, please refer to this blog post.

When binding data to the RadGrid, ensure that the DataKeyNames property is set to the name of your ID column. In the ProductTable class, that field’s name is ProductIdenity and that property can be set on the RadGrid by going to MasterTableView > DataKeyNames. More detailed instructions on using the RadGrid to bind data can be found in the RadControls for ASP.NET AJAX self-paced tutorial.

 

Once you create your data-bound control, you can easily upload it as a user control to Sitefinity. For more info, please read this article.  

5 comments

Leave a comment
  1. Gabe Sumner Dec 15, 2008
    Thanks Joseph.  Several additional OpenAccess related articles can be found on the official Telerik Blogs and on sitefinitywatch.com.
  2. Ben Dec 19, 2008
    I mentioned this on sitefinitywatch.com as well. But one of the cool parts about Nolics was that you didn't have to write every private member and public class. You could use their scripting language to gererate the classes. This made it easy to do things like set the max size of a field, set a field to nullable, create groups of items, and create relations. While I haven't played with OA, is there a wizard or scripting language that could speed up the creation of the classes as they get more complex.
  3. Joseph Anderson Dec 22, 2008
    Open Access does have a wizard for writing some of the code, such as making the primary key and making classes persistent. Here is a link for more info.




  4. Himadri Dec 31, 2008
    I have created 10 views in my SqlServer 2005 database.
    I need to know How can I create Persistant class for those view or How can I retrieve data from
    those views. Please suggest.
  5. Joseph Anderson Jan 20, 2009
    Hi Himadri. Right now, OpenAccess does not map views and stored procedures. For the Q1 release, which is due in February, OA will support mapping stored procedures. Liekly for the Q2 2009 release, which is due in June, OA will support mapping of views. Right now, you could turn those views into tables, then run the reverse mapping wizard to map them. For more info, please take a look at this KB article.

    Leave a comment