Walkthrough: Creating Domain Model
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
In the previous topic was demonstrated how to migrate the SofiaCarRental database from SQL Server to SQL Azure. In this task you will learn how to create a new Telerik Data Access Domain Model and connect it to the SofiaCarRental database on SQL Azure.
- Open Microsoft Visual Studio 2010 in elevated administrator mode.
- From the File menu, choose New and then Project.
- In the New Project dialog, expand the language of your preference. Select Windows and then select Windows Forms Application. Type GettingStartedOASqlAzure for a name of your project.
- Click OK to create the project. Next you will add a new Telerik Data Access Domain Model and configure it to read data from SQL Azure.
- Right-click the GettingStartedOASqlAzure, point Add and then New Item.
- In the Add New Item dialog box, choose Data and then Telerik Data Access Domain Model. Click Add.
-
The Select Domain Model Type Dialog appears.
- Select the Populate from database domain model type.
- From the Backend drop-down, choose Microsoft SQL Azure.
-
Specify SofiaCarRentalDbContext for name of the model.
Click Next to continue. The Setup Database Connection Dialog appears. The most tricky moment when connecting Telerik Data Access Domain Model with SQL Azure is in the Data Connection Dialog. You need to set up the connection string manually.
-
In the Connection String field enter the following connection string.
Server=tcp:*server_name*.database.windows.net;Database=*SofiaCarRental*;User ID=*user_name*@*server_name*;Password=*myPassword*;Trusted_Connection=False;Encrypt=True;
You should specify the following parameters in the connection string:
- server_name - the name of your assigned server.
- user_name - this is a valid logon user granted to use the SofiaCarRental. By default you could use the database master.
- myPassword - your password.
Click Next to proceed. The rest of the actions in this topic are pretty familiar for all Telerik Data Access developers.
- In the Choose Database Items Dialog select only the Cars table and click Finish to generate the model.
To test that the generated OpenAccessContext works correctly and loads data from SQL Azure, you need to create a simple user interface for the main form and display some data.
- Open the Form1.cs class. Drag a new DataGridView control to the form.
-
Switch to the code-behind and declare a new instance of the SofiaCarRentalDbContext.
private SofiaCarRentalDbContext dbContext = new SofiaCarRentalDbContext();
Private dbContext As New SofiaCarRentalDbContext()
-
Add the following code in the constructor of the form. All records from the Cars table is loaded and displayed in the DataGridView.
public Form1() { InitializeComponent(); dataGridView1.DataSource = dbContext.Cars.ToList(); }
Public Sub New() InitializeComponent() dataGridView1.DataSource = dbContext.Cars.ToList() End Sub
-
Run your application to ensure that the data is loaded.