Azure and SQL Database Tutorials - Tutorial 2.1: Create an OData Service
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:
- Complete the steps in Tutorial 2 Using SQL Database
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: Define the Entity Framework data model
- Lesson 2: Modify the application to add the data service
- Lesson 3: Test and deploy the application
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:
- Open the golfer message board application
- Create the Entity Data Model to access the GolferMessageBoardDB on SQL Database
- Compile the MessageBoard_WebRole project
To open the Golfer Message Board application
- Click Start, point to All Programs, point to Microsoft Visual Studio 2012, right-click Visual Studio 2012, and then click Run as administrator.
- If the User Account Control dialog appears, click Yes.
- Click the File menu, point to Open, and then click Project/Solution.
- In File name, type C:\AzureTutorials\Tutorial2.1\GolferMessageBoard\GolferMessageBoard.sln, and the click Open.
To create the Entity Data Model
In Solution Explorer, right-click MessageBoard_WebRole, point to Add, and then click New Item.
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
Click Add
In the Entity Data Model Wizard, select Generate from Database, and then click Next.
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
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
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
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.
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).
In the Entity Designer surface, right-click the Message entity, and click Properties.
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:
- Add the data service code files to the project
- Modify the data service to use the entity data model from the previous lesson
- Compile the MessageBoard_WebRole project
To add WCF Data Services code files to the project
- In Solution Explorer, right-click MessageBoard_WebRole, point to Add, and then click New Item.
- 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
- 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
In MessageBoard_WebRole, right-click MessageBoard.svc, and click View Code.
In the code for the data service, locate the the following comment in code:
/* TODO: put your data source class name here */
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>
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:
- Disable feed reading view in the browser
- Test the application in compute emulator
- Generate the service package
- Sign in to Windows Azure
- Create a cloud service
- Deploy the application to the staging environment
- Test the application in the staging environment
- Promote the application to the production environment
To disable feed reading view in Internet Explorer
Open Internet Explorer.
Click Tools menu, and then select Internet Options.
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
If MessageBoard is not the startup project: In Solution Explorer, right-click MessageBoard, and then click Set as Startup Project.
Right-click Message.svc and click Set As Start Page.
From the Debug menu, click Start Debugging.
Switch to the browser window to view the service document for the OData service.
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.
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:
- Test the application in compute emulator
- Generate the service package
- Sign in to Windows Azure
- Create a cloud service
- Deploy the application to the staging environment
- Test the application in the staging environment
- 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
- In Solution Explorer, right-click the MessageBoard cloud project, and then click Package.
- In Packinage Windows Azure Application, select the following values:
Name Value Service configuration
Cloud Build configuration Release - 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
Open a Web browser and browse to http://windows.azure.com/. This is the Windows Azure Management Portal.
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 [[articles: Windows Azure and SQL Database Tutorials - Tutorial 1: Using Windows Azure Web Role and Windows Azure Table Service|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
- From the portal, in the left pane, click CLOUD SERVICES.
- On the bottom left corner of the portal page, click NEW, click COMPUTE, click CLOUD SERVICE, and then click CUSTOM CREATE.
- 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) - 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
- From the portal, in the left pane, click CLOUD SERVICES.
- In the middle pane, click the cloud service you just created. The default name is <yourname>gmb..
- From the top of the page, click STAGING.
- Click UPLOAD A NEW STAGING DEPLOYMENT or UPDATE if you have deployed a Golfer Message Board application from other tutorials before.
- 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.cspkgConfiguration file C:\AzureTutorials\Tutorial2.1\GolferMessageBoard\MessageBoard\bin\Release
\app.publish\ServiceConfiguration.Cloud.cscfgDeploy 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) - Click the check sign on the bottom right corner of the page.
- Wait until the upload process is completed. The process can take several minutes to complete.
To test the application in the staging environment
- From the portal, in the left pane, click CLOUD SERVICES.
- In the middle pane, click the cloud service you created.
- From the top of the page, click STAGING.
- On the right side of the page, click the site URL.
- 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
From the portal, in the left pane, click CLOUD SERVICES.
In the middle pane, click the cloud service you created.
From the top of the page, click STAGING.
On the bottom of the page, click SWAP.
On the right, click YES.
On the top of the page, click PRODUCTION. It takes several minutes to complete the operation.
On the right, click the SITE URL.
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.
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:
- WCF Data Services Client Library – used to consume an OData feed in a .NET Framework client application.
- WCF Data Services (Silverlight) – used to consume an OData feed in a Silverlight client application.
- Open Data Protocol (OData) Client for Windows Phone – used to consume an OData feed in a Windows Phone client application
For a complete list of client libraries that support OData, see the OData SDK.
Return to Top
See Also
- [[Windows Azure and SQL Database Tutorials (en-US)]]
- Windows Azure and SQL Database Tutorials - Tutorial 1: Using Windows Azure Web Role and Windows Azure Table Service (en-US)
- Windows Azure and SQL Database Tutorials - Tutorial 2 Using SQL Database
- Windows Azure and SQL Database Tutorials - Tutorial 3 Using Windows Azure Blob Service
- Windows Azure and SQL Database Tutorials - Tutorial 4 Using Windows Azure Worker Role and Windows Azure Queue Service
- [[Using the Windows Azure Web Role and Windows Azure Table Service with PHP]]
- [[Using the Windows Azure Web Role and SQL Database with PHP]]
Return to Top