Walkthrough: Creating A Model
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 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 Class Library. Type GettingStartedAzure for a name of your project.
- Integrate the project with the Telerik.DataAccess.Fluent NuGet package.
-
In the App.config file, add a connection string to your Azure database:
<connectionStrings> <add name="AzureConnection" connectionString="Server=tcp:*server_name*.database.windows.net; Database=*SofiaCarRental*; User ID=*user_name*@*server_name*; Password=*myPassword*; Trusted_Connection=False; Encrypt=True;" providerName="System.Data.SqlClient" /> </connectionStrings>
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.
Create POCO classes for the persistent classes of SofiaCarRental.
- Using the Fluent Mapping API create the metadatasource class where the mapping is defined.
-
Create your context class:
-
Configure the backend to be Azure and to use the System.Data.SqlClient provider.
private static BackendConfiguration backend = GetBackendConfiguration(); public static BackendConfiguration GetBackendConfiguration() { BackendConfiguration backend = new BackendConfiguration(); backend.Backend = "Azure"; backend.ProviderName = "System.Data.SqlClient"; CustomizeBackendConfiguration(ref backend); return backend; }
Private Shared backend As BackendConfiguration = GetBackendConfiguration() Public Shared Function GetBackendConfiguration() As BackendConfiguration Dim backend As BackendConfiguration = New BackendConfiguration() backend.Backend = "Azure" backend.ProviderName = "System.Data.SqlClient" CustomizeBackendConfiguration(backend) Return backend End Function
-
Add the name of the connection string:
private static string connectionStringName = @"AzureConnection";
Private Shared connectionStringName As String = "AzureConnection"
-
Create a new instance of your FluentMetadataSource class that holds the mapping:
private static MetadataSource metadataSource = new MyModelFluentMetadataSource();
Private Shared metadataSource As MetadataSource = New MyModelFluentMetadataSource()
-
Create a constructor for the context:
public MyModel() :base(connectionStringName, backend, metadataSource) { }
Public Sub New() MyBase.New(connectionStringName, backend, metadataSource) End Sub
-
Expose the persistent classes through read-only IQueryable<T> properties of the context. For example:
public IQueryable<Car> Cars { get { return this.GetAll<Car>(); } }
Public ReadOnly Property Cars() As IQueryable(Of Car) Get Return Me.GetAll(Of Car)() End Get End Property
-
To test that the model, you can use a simple console application that should be configured as described in the Consuming a Model - Configuration article.