Overview


This tutorial demonstrates the process of creating a data service application that implements the Open Data Protocol (OData) to expose data from Azure SQL Database and the process of deploying the application to Azure.
 Note
If you wish to contribute to this page, use the Edit tab at the top (sign-in required). If you wish to provide feedback for this documentation please either send e-mail to azuredocs@microsoft.com or use the Comment field at the bottom of this page (sign-in required).

Last reviewed: 11/04/2012

In tutorial 1 you created a simple golfer message board application. In tutorial 2 you updated the application to store message board data, which includes a name and a message, in a SQL Database.

This tutorial is based on these two previous tutorials. In this tutorial you extend the Web application to add a data service that implements the  Open Data Protocol (OData). This OData service uses the SQL Database created in tutorial 2.

Note: The tutorial code has been tested on Windows Azure SDK (October 2012).

Objectives

In this tutorial, you will learn how to:

  • Create an Entity Framework data model to access SQL Database-hosted database
  • Use WCF Data Services with the Entity Framework provider to create an OData service
  • Deploy a WCF Data Services Web application to Windows Azure

Prerequisites

Note the following requirements before you begin this lesson:

Understanding the Architecture

The data service runs in the Windows Azure Web role and uses Entity Framework to access data from the SQL Database service. For more general information on the Windows Azure and SQL Database architecture used in this application, see the section Understanding the Architecture  in tutorial 2.

In this Article


Lesson 1: Modify the Application to Define the Entity Framework Data Model

In tutorial 2, the application accessed the GolferMessageBoardDB database by using a data source based on LINQ-to-SQL classes.

You need to use the Entity Data Model wizard to add an Entity Framework data model to the project. This data model will be used by the data service to access the database. When you create a new Entity Data Model in your project, the wizard also adds the appropriate assembly references to the project.

Procedures

In this lesson, you will go through the following procedures:

  1. Open the golfer message board application
  2. Create the Entity Data Model to access the GolferMessageBoardDB on SQL Database
  3. Compile the MessageBoard_WebRole project

To open the Golfer Message Board application

  1. Click Start, point to All Programs, point to Microsoft Visual Studio 2012, right-click Visual Studio 2012, and then click Run as administrator.
  2. If the User Account Control dialog appears, click Yes.
  3. Click the File menu, point to Open, and then click Project/Solution.
  4. In File name, type C:\AzureTutorials\Tutorial2.1\GolferMessageBoard\GolferMessageBoard.sln, and the click Open.

To create the Entity Data Model

  1. In Solution Explorer, right-click MessageBoard_WebRole, point to Add, and then click New Item.
  2. In the Add New Item – MessageBoard_WebRole dialog, type and select the following values:
    Name Value

    Installed Templates

    Visual C# , Data

    Template

    ADO.NET Entity Data Model

    Name

    MessageBoard.edmx

  3. Click Add
  4. In the Entity Data Model Wizard, select Generate from Database, and then click Next.
  5. In the Choose Your Data Connection screen of the Entity Data Model Wizard dialog, type and select the following values:
    Name Value

    Which data connection should your application use to connect to the database?

    GolferMessageBoardDBConnectionString

    Select the option:

    Yes, include the sensitive data in the connection string

    Save entity connection settings in Web.config as:

    GolferMessageBoardDBEntities

  6. Click Next.

    Security Note: To properly secure the SQL Database connection string, you should encrypt the Web.config file. Encrypting the Web.config file is outside the scope of this tutorial. For an example of how to do this, see the blog series on  Windows Azure SQL Database

  7. In the Choose Your Database Objects and Settings screen of the Entity Data Model Wizard dialog, select the following values:
    Name Value

    Enable these options:

    • Tables
    • Pluralize or singularize generated object names
    • Include foreign key columns in the model
    Model Namespace

    GolferMessageBoardDBModel

  8. Click Finish. This adds the MessageBoard.edmx file and references to Entity Framework assemblies to the project. The MessageBoard.edmx file represents the data model and mapping to the SQL Database.
  9. In Solution Explorer, expand the MessageBoard_WebRole project and double-click the MessageBoard.edmx file. This opens the data model in the ADO.NET Entity Data Model Designer (Entity Designer).
  10. In the Entity Designer surface, right-click the Message entity, and click Properties.
  11. In the Properties window, update the following property values:
    Name Value

    Name

    Message2

    Entity Set Name

    Messages

    Note: By default, both the Object Relational Designer (LINQ-to-SQL) and the Entity Designer (LINQ-to-Entities) generate the Message type in the default project namespace. To prevent  errors when building the project, you need to rename one of the types to Message2. The entity set name is used by WCF Data Services as the name of the feed, which is changed to Messages.

     To compile the MessageBoard_WebRole project

    • In Solution Explorer, right-click MessageBoard_WebRole, and then click Rebulid. Make sure the project is compiled successfully.

    What did I just do?

    In this step, you modified the golfer message board application to add an Entity Framework data model. This data model and generated classes are used to connect to SQL Database by using the Entity Framework.

    Next Steps:

    You will use the WCF Data Services item template to add the code to the project that defines the OData service. This data service uses the new Entity Framework data model.

    Return to Top


    Lesson 2: Modify the Application to Add the Data Service

    In this lesson, you add the code for the OData service, which uses the data model created in the previous lesson.

    This template adds both a markup page (.svc) and a code-behind page to the project. It also adds the appropriate assembly references to the project.

    Note: When you create a data service such that the generic type of the DataService<T> class is the strongly-typed ObjectContext of an Entity Framework data model, WCF Data Services uses the Entity Framework provider to access and change data in the database. For more information, see Entity Framework Provider (WCF Data Services).

    Procedures

    In this lesson, you will go through the following procedures:

    1. Add the data service code files to the project
    2. Modify the data service to use the entity data model from the previous lesson
    3. Compile the MessageBoard_WebRole project

    To add WCF Data Services code files to the project

    1. In Solution Explorer, right-click MessageBoard_WebRole, point to Add, and then click New Item.
    2. In the Add New Item – MessageBoard_WebRole dialog, type and select the following values:
      Name Value

      Installed Templates

      Visual C# , Web

      Template

      WCF Data Service

      Name

      MessageBoard.svc

    3. Click Add. This adds both the data service code files and WCF Data Services assembly references to the project.

    To modify the WCF Data Services code files to use the data model

    1. In MessageBoard_WebRole, right-click MessageBoard.svc, and click View Code.
    2. In the code for the data service, locate the the following comment in code:
      /* TODO: put your data source class name here */
    3. Replace this comment with the name of the strongly-typed ObjectContext of the Entity Framework data model, which is GolferMessageBoardDBEntities. The class definition should look like the following:
      public class MessageBoard : DataService<GolferMessageBoardDBEntities>
    4. Locate the InitializeService method and replace it with the following code:
      // This method is called only once to initialize service-wide policies.
      public static void InitializeService(DataServiceConfiguration config)
      {
         // Grant only the rights needed to support client applications,
         // in this case read all and insert new messages.
         config.SetEntitySetAccessRule("Messages", EntitySetRights.AllRead |
         EntitySetRights.WriteAppend); 
      }

    To compile the MessageBoard_WebRole project

    • In Solution Explorer, right-click MessageBoard_WebRole, and then click Rebulid. Make sure the project is compiled successfully.

    What did I just do?

    In this step, you used WCF Data Services to add an OData feed to the golfer message board application, which accesses the SQL Database by using and the Entity Framework provider. This enables you to read messages as OData feeds and insert new messages by sending POST requests to the OData service.

    Next Steps:

    You will test and deploy the application.

    Return to Top


    Lesson 3: Test and Deploy the Application

    In this lesson, you test the application in Windows Azure computer emulator, package the application, and then deploy the application to Windows Azure.

    Note: You must disable feed reading view in the browser to view the raw Atom XML feed returned from the OData service.

    Procedures

    In this lesson, you will go through the following procedures:

    1. Disable feed reading view in the browser
    2. Test the application in compute emulator
    3. Generate the service package
    4. Sign in to Windows Azure
    5. Create a cloud service
    6. Deploy the application to the staging environment
    7. Test the application in the staging environment
    8. Promote the application to the production environment

    To disable feed reading view in Internet Explorer

    1. Open Internet Explorer.
    2. Click Tools menu, and then select Internet Options.
    3. Click the Content tab, click Settings in the Feeds and Web Slices section, and clear Turn on feed view.

      Note: If you are using a Web browser other than Internet Explorer and if your browser cannot display the feed as raw XML data, you should still be able to view the feed as the source code for the page.

    To test the application in the computer emulator

    1. If MessageBoard is not the startup project: In Solution Explorer, right-click MessageBoard, and then click Set as Startup Project.
    2. Right-click Message.svc and click Set As Start Page.
    3. From the Debug menu, click Start Debugging.
    4. Switch to the browser window to view the service document for the OData service.
    5. In the address bar, append the entity set name Messages to the root URI of the data service, and then press return. This displays the Messages OData feed, which returns all messages in the database.

      Note: If the feed returned appears empty, then you will need to first insert some data into the SQL Database. The easiest way to add new messages to the database is by entering them in the default.aspx page of the Web application.

    6. Close the browser window to stop the debugger and shut down the development in the compute emulator.

    After the application is tested successfully in the compute emulator environment, the next step is to create the service package and then deploy the application to Windows Azure.

    Procedures

    In this lesson, you will go through the following procedures:

    1. Test the application in compute emulator
    2. Generate the service package
    3. Sign in to Windows Azure
    4. Create a cloud service
    5. Deploy the application to the staging environment
    6. Test the application in the staging environment
    7. Promote the application to the production environment

    After the application is tested successfully in the compute emulator environment, the next step is to create the service package and then deploy the application to Windows Azure.

    To generate the service package

    1. In Solution Explorer, right-click the MessageBoard cloud project, and then click Package.
    2. In Packinage Windows Azure Application, select the following values:
      Name Value

      Service configuration

      Cloud
      Build configuration Release
    3. Click Package. After Visual Studio builds the project and generates the service package, Windows Explorer opens with the current folder set to the location where the generated package is stored. The default directory is C:\AzureTutorials\Tutorial2.1\GolferMessageBoard\MessageBoard\bin\Release\app.publish. Make sure to write down the path. You will need it later in this lesson.

    To sign in to Windows Azure

    1. Open a Web browser and browse to http://windows.azure.com/. This is the Windows Azure Management Portal.
    2. Sign in using the Windows Live ID associated with your Windows Azure account.

      Note: If you haven’t had a Windows Azure Platform subscription, see the Provisioning Windows Azure section of this tutorial.

    You need to create a cloud service for the Web role. If you have created a cloud service account in tutorial 1 or [[Windows Azure and SQL Database Tutorials - Tutorial 2: Using SQL Database (en-US)|tutorial 2]], you can skip this step.

    To create a cloud service

    1. From the portal, in the left pane, click CLOUD SERVICES.
    2. On the bottom left corner of the portal page, click NEW, click COMPUTE, click CLOUD SERVICE, and then click CUSTOM CREATE.
    3. Type or select the following values.
      Name Value
      URL <yourname>gmb

      Note: The URL prefix must be unique.

      REGION/AFFINITY GROUP (For better performance, select the same region as the one you chose for the storage service, or use an affinity group.)
      Deployment a cloud service package now (not selected)
    4. Click the check sign on the bottom right corner. Wait until the status changes to Created. This process could take several minutes.

    To deploy the application to the staging environment

    1. From the portal, in the left pane, click CLOUD SERVICES.
    2. In the middle pane, click the cloud service you just created.  The default name is <yourname>gmb..
    3. From the top of the page, click STAGING.
    4. Click UPLOAD A NEW STAGING DEPLOYMENT or UPDATE if you have deployed a Golfer Message Board application from other tutorials before.
    5. Type or select the following value.
      Name Value
      Deployment name v1.1.1.0
      Package location C:\AzureTutorials\Tutorial2.1\GolferMessageBoard\MessageBoard\bin\Release
      \app.publish\MessageBoard.cspkg
      Configuration file C:\AzureTutorials\Tutorial2.1\GolferMessageBoard\MessageBoard\bin\Release
      \app.publish\ServiceConfiguration.Cloud.cscfg
      Deploy even if one or more roles contain a single instance (Selected).  You can always increase the number of instances from the portal.
      Start deployment (only available when deploying a new application) (Selected)
    6. Click the check sign on the bottom right corner of the page.
    7. Wait until the upload process is completed. The process can take several minutes to complete.

    To test the application in the staging environment

    1. From the portal, in the left pane, click CLOUD SERVICES.
    2. In the middle pane, click the cloud service you created.
    3. From the top of the page, click STAGING.
    4. On the right side of the page, click the site URL.
    5. Test the application by entering one or more entries.

    After the application is working correctly in the staging environment, you are ready to promote it to the production environment.

    To promote the application to production

    1. From the portal, in the left pane, click CLOUD SERVICES.
    2. In the middle pane, click the cloud service you created.
    3. From the top of the page, click STAGING.
    4. On the bottom of the page, click SWAP.
    5. On the right, click YES.
    6. On the top of the page, click PRODUCTION. It takes several minutes to complete the operation.
    7. On the right, click the SITE URL.
    8. In the Properties pane, click the URL in the DNS name box. The application is opened in a new browser tab or a new browser window depending on your browser configuration.

      Note: Some DNS services take longer to replicate the records. If you get a page not found error, you might need to try browsing to the URL again in a few minutes.

    9. Test the OData feed in the production environment by accessing the Messages feed as before.

    What did I just do?

    In this step, you tested and deployed the golfer message board OData service to Windows Azure.

    Next Steps:

    Congratulations!  You have completed the tutorial 2.1. Now that you have deployed the data service, you can create client applications that consume the Messages OData feed. For more information on how to create client applications that access OData services, see one of the following topics in the MSDN library:

    For a complete list of client libraries that support OData, see the OData SDK.

    Return to Top


    See Also

    Return to Top