This series of videos explain how to create your model with Telerik OpenAccess ORM, and then create database tables from the .NET classes. This mapping approach is better known as Model-first approach or Forward Mapping approach.
In this video you will learn how to get started building a web site using the forward mapping(Model First) capabilities available in OpenAccess ORM. In addition you will learn how to extend the data model to include custom properties, and bind the data model to a RadGrid using the OpenAccessLinqDataSource.
Telerik OpenAccess ORM gives you the ability to create a conceptual model first and then derive the storage model (the database). This topic will walk you through each step of generating a conceptual model, and will explain all the different properties and components involved in the model-first approach. OpenAccess ORM model first mapping (also known as forward mapping) allows developers to concentrate on building classes, and will automatically generate a database based on those classes.
Creating the Project
To create a new Telerik OpenAccess Domain Model using model first mapping:
- Select File > New Project in Visual Studio.
- In the list of Installed Templates on the left side of the dialog, select Visual C# or Visual Basic.
- Then select Telerik OpenAccess Class Library. Give the new project a name, and then click OK.
- This will start the OpenAccess Create Model Wizard. The first step in the wizard lets you choose the type of model you want to use. In this step, you have the option of generating a model from a database or starting with an empty model. This topic deals with generating a model first and deriving the database, so select the Empty Domain Model option as shown on the figure below.
- Select the backend provider (the default backend is MSSQL). Give the model a name or use the default one, and click Finish. For more information about the supported database, check out here.
What Just Happened?
When you click finish, the wizard will enhance the project to work with OpenAccess. In addition, it will add the required references, and an .rlinq file to the project.
||To see the generated domain classes in VB.NET projects, use the Show All Files command from the Solution Explorer toolbar.|
Building the Model
The next step is to build your domain model. Double click the rlinq file to open the Visual Designer. The designer, when empty, contains a simple message, which states that to create new entities you need to drag them from the Toolbox. Just like a normal Visual Studio development, the items you want to place on your designer are found in the Toolbox. The Toolbox contains, besides the Pointer, several controls, or items, from which to design your domain model.
Creating an Entity
For this demo, you will need to create one entity. You can create entities by dragging the Domain Class object from the toolbox into the Visual Designer. Drag a Domain Class item from the toolbox into the Visual Designer. This will create a new entity called DomainClass1, without any properties, as shown on the figure below.
Renaming the Entity
Select the entity in the Visual Designer and rename it to Customer (by pressing the F2 key).
The next step is to define properties for the Customer entity. Properties can be added by right-clicking on the entity and selecting Add -> Property.
When you add a new property, the new property is initially in edit mode. You could change its name and type. Each time you add a new property, the default type for the property is System.String. You could change the property type by entering the type directly. Another way to change the property is to select it and then press F4. This will open the Property Window. There, you could change the Name and Type properties.
Define four properties for the Customer entity:
- Id : Int32
- Name : String
- DateCreated : DateTime
- EmailAddress : String
Setting a Primary Key
Each time you add an entity to the Visual Designer, you will have to define a primary key column/property. In this demo, the Customer.Id property will be used as a primary key. Select the Id property and press F4 to open the Properties pane. Change the Identity property to True.
||If you forget to specify a primary key, Telerik OpenAccess ORM will automatically generate a primary key column during the database update.|
Mapping an Entity to a Table
The model is finished, but when you save it and take a look at the Error List pane, you should be presented with the following warning.
This warning is completely valid, and the information is absolutely correct. The warning is letting you know that the Customer item in the Visual Designer is not mapped to anything. The project will save and compile as-is, since this is a warning, but beyond having your conceptual model you really don't have anything substantial.
Here is the moment where the model first feature of Telerik OpenAccess ORM comes into play. The next step is to map the Customer entity to a table. Select the Customer entity in the Visual Designer and open the Mapping Details Editor (View->Other Windows->Entity Diagrams Details Editor). In the Table Mappings section, check the Use Default Mapping option.
In this demo, default mapping will be used, i.e. Telerik OpenAccess ORM will take care about the property-to-column mapping. Or in other words, Telerik OpenAccess ORM will generate automatically the names of the corresponding database columns. Now that the domain model is created, the last step in the process is to create the database OpenAccess will use.
Deploying the Database
OpenAccess will generate database creation, or migration scripts based on the changes made in the designer. In addition, it can execute these scripts, making database creation, and migration very simple. Right click anywhere on the surface of the Visual Designer. Select the Update Database from Model command. This will start the Update Database from Mode lwizard.
The first page in the wizard lets you specify the data connection for your domain model. If you have previously created connections they will show up in the drop-down. You will need to create a new database named MyDatabase, and this can be done by clicking the New Connection button. This will open the standard Connection Properties dialog. The Connection Properties dialog looks very similar to most of the other SQL Server connection dialogs in which you need to provide the server name, authentication method, and the database name. Select your server and type MyDatabase in the database name field. Click OK to close the dialog. If MyDatabase does not exist on the server, then you will be prompted with a dialog asking you whether you want to create the database. Click Yes to create the database.
You will get taken back to the data connection page of the wizard with your new connection shown in the connection drop-down. You have to specify the name of the connection string. This should be a unique name, which identifies the connection string in the <connectionStrings> </connectionStrings> section of the App.Config or Web.Config file (depending on the project type).
Click Next to proceed. On the next page you have to specify the update strategy. Since you are initializing your database schema for first time, select the Create Database option. However once your database schema is initialized, each time you want to update it again, you have to use the Migrate Database.
Click Next. On the Summary Page, you could preview and execute the generated script. Select the Create script file and execute option and click Finish.
After clicking finish, OpenAccess ORM will save a copy of the script, and execute it against the database.
If you take a look at the Solution Explorer, you should see that two new files are added in your project - App.config file and a file containing the generated script. The configuration file contains your connection string to the target database.
Open the script file. Here is the entire DDL script. As you can see by this DDL, the script is very thorough. It takes care of handling any existing relationships and keys.
CREATE TABLE [Customer] (
[nme] varchar(255) NULL, -- _name
[Id] INT NOT NULL, -- _id
[EmailAddress] varchar(255) NULL, -- _emailAddress
[DateCreated] datetime NOT NULL, -- _dateCreated
CONSTRAINT [pk_Customer] PRIMARY KEY ([Id])
In this topic, you learnt how to perform the following tasks with Telerik OpenAccess ORM:
- Create a new OpenAccess Class Library project. Telerik OpenAccess ORM provides various project templates, allowing you to create new projects based on OpenAccess ORM in just few clicks. Read more
- Work with the OpenAccess Create New Model Wizard. The wizard allows you to generate a new domain model. Read more
- Working with the Visual Designer. You learnt how to create new entities, define properties, set a primary key, using default mapping. All these tasks are described in details in the Model Definition Basics section.
- Deploying the database - you used the Update Database from Model wizard to create a new database from an existing domain model. The wizard also allows you to migrate an existing database to the latest model state. Read more
- At this point the model is ready to be connected up to the UI, or run the Data Services Wizard to create a service layer over the model. Read more