Community & Support
Home / Community & Support / Knowledge Base / Telerik OpenAccess ORM / General / How to accomplish forward mapping to a database using Telerik OpenAccess ORM

How to accomplish forward mapping to a database using Telerik OpenAccess ORM

Article Info

Rating: 4

Article information

Article relates to

 Telerik OpenAccess ORM

Created by

 Alexander Filipov

Last modified

 December 15, 2008

Last modified by

 Alexander Filipov


DESCRIPTION
This article explains how to make your application classes persistent on the fly by using the Open Access O/R mapper tool. The process of having domain objects in our application scope that have to be persisted, but there is no database schema defined for them in advance is called ‘Forward Mapping’.

SOLUTION
For the purpose of this article we will use two classes named Order and OrderDetail. The first represents order entry with some basic fields like customer name, date of order, shipping address and a list of products to be delivered. The OrderDetail class is used to describe the products that are enlisted for every order inside the details collection. It contains the product name, its price and the requested quantity Note that default constructor (no arguments) is required!

    public class Order  
    {  
        private string customer;         
        private DateTime orderDate;  
        private string shipAddress;  
        private IList<OrderDetail> details = new List<OrderDetail>();  
 
        public Order()  
        {  
        }  
 
        public string Customer  
        {  
            get { return customer; }  
            set { customer = value; }  
        }  
 
        public DateTime OrderDate  
        {  
            get { return orderDate; }  
            set { orderDate = value; }  
        }  
 
        public string ShipAddress  
        {  
            get { return shipAddress; }  
            set { shipAddress = value; }  
        }  
 
        public IList<OrderDetail> Details  
        {  
            get { return details; }  
            set { details = value; }  
        }  
    }  
 
    public class OrderDetail  
    {  
        private string product;        
        private float price;  
        private int quantity;  
 
        public OrderDetail()  
        {  
        }  
 
        public float Price  
        {  
            get { return price; }  
            set { price = value; }  
        }  
 
        public string Product  
        {  
            get { return product; }  
            set { product = value; }  
        }  
 
        public int Quantity  
        {  
            get { return quantity; }  
            set { quantity = value; }  
        }  
    }  
 

Now when we have the domain model classes it is time to enhance the project so that it could be handled by OpenAccess ORM. The OpenAccess Enable Project wizard will help us to do that:



You can do that also by right clicking on the project’s name inside the Solution Explorer, and in the context menu select OpenAccess -> Enable Project.

Then the OpenAccess wizard will show up:



Click ‘Next’ and move to the next step.



Make sure the ‘Persistent classes’ check box is checked. I recommend you to check the second check box as well. This will enable the creation of an ObjectScopeProvider helper class which provides a preset object context (IObjectScope instance) to the database we are going to  use.

The third step is very important. Here you select the database server and define the connection settings. If a database with the desired name already exists it will be used, otherwise a new  database will be created during the build process. Note that all these settings can be changed later in the ‘App.config’ file or by running the Enable Wizard again.





Now the project is prepared for using the OpenAccess ORM. Next step is to setup the classes we want to persist.

First make sure you have included the ‘using Telerik.OpenAccess;’ clause in the class definition file. To be marked as persistent the [Persistent] attribute should be put above each class definition.

[Persistent]  
public class Order  
{  
    …  
}  

This could be done through the Forward Mapping Wizard as well.



Open the wizard and on the left you will see all the classes in the project. If something is missing just hit the refresh button above. Select a class you want to make persistent and check  the ‘Make this class persistent’ check box on the right side of the form. If you have opened the class definition file in the background you will probably notice that the [Persistent] attribute is immediately added to the class. Do not close the wizard yet.



Now we need to define the relations between our classes. In this example the ‘Order’ class has a List of ‘OrderDetail’ entries. The relationship type is ‘1 to many’. After we made the ‘Order’ class persistent all the persisted fields will be showed immediately. Select the ‘details’ field. In most cases you will not have to change the default options on the right side of the form. However we will check the ‘Cascading delete’ option. A [Depend] attribute is added above the details list field definition. That will cause the deletion of all order details when an order is being deleted. We require this batch update because it does not make sense to have orphan ‘OrderDetail’ entries without an Order parent.



Press ‘Done’ and you are ready to build the project. All necessary tables and relations in the database will be made available automatically by the ORM tool. In the output of the build process you will see an output similar  to this:

========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========  
========== OpenAccess All: 1 succeeded, 0 failed, 0 skipped ========== 

Now you are ready to use your persistent objects.

Comments

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.