Data Access has been discontinued. Please refer to this page for more information.

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.

  1. Open Microsoft Visual Studio 2010 in elevated administrator mode.
  2. From the File menu, choose New and then Project.
  3. 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.
  4. Integrate the project with the Telerik.DataAccess.Fluent NuGet package.
  5. 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.
  6. Create POCO classes for the persistent classes of SofiaCarRental.

  7. Using the Fluent Mapping API create the metadatasource class where the mapping is defined.
  8. Create your context class:

    1. 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
      
    2. Add the name of the connection string:

        private static string connectionStringName = @"AzureConnection";
      
        Private Shared connectionStringName As String = "AzureConnection"
      
    3. 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()
      
    4. Create a constructor for the context:

        public MyModel()
            :base(connectionStringName, backend, metadataSource)
        { }
      
        Public Sub New()
            MyBase.New(connectionStringName, backend, metadataSource)
        End Sub
      
    5. 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.

See Also