Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 1) - Generating an Object Model to Access the Cloud Database
You will be creating a WCF service called NotepadService that will be consumed by the Windows Phone Notepad application. Think of NotepadService as the layer that provides managed APIs to the Notepad application. NotepadService will utilize the Entity Framework to generate object models based on the database tables, and it will also generate a persistence layer that performs the database operations, which otherwise you would have to code yourself. Finally, the steps will provide you with instructions on creating and deploying NotepadService to Windows Azure. You will be creating a WCF Azure service and running it from your machine, and then you will learn to package and deploy the project to the Azure cloud, where you will be able to configure to have multiple services run if your application demand increases.
1. Creating a Windows Azure Project
You will be creating a Windows Azure NotepadService project in Visual Studio in the following steps. In order to create Azure services, you would need to download Azure tools and SDK from www.microsoft.com/windowsazure/windowsazure/default.aspx.
Create a new Windows Phone Application by selecting File => New Project on the Visual Studio command menu. Select the Cloud installed template on the left, and choose Windows Azure Cloud Service from the list on the left, as shown in Figure 1. Name the Azure service "NotepadService" and click OK.
Figure 1. Windows Azure Cloud Service project
You will be prompted to select the type of role. Notice here that if you want to host the web project, you would need to select ASP.NET Web Role. For Notepad WCF service, you will need to select WCF Service Web Role, as shown in Figure 3-18, and click the arrow pointing to the left. In Cloud Service Solution, you will see WCFServiceWebRole, and if you hover your mouse over the item, you will see that a little pencil icon appears. Click the pencil icon and change the name to NotepadServiceRole, as also shown in Figure 2.
Figure 2. Selecting WCF Service Web Role
2. Generating an Object Model to Access the Cloud Database
Now that we have basic plumbing for implementing a WCF service, it is a good time to implement a persistence layer that allows you to interact with the database. Entity Framework will act as an object-relational mapping tool that will take database tables and create equivalent object models and many of the tedious tasks of coding methods, like add, delete, update, and search, which can be easily handled by Entity Framework.
When you complete this section, you will be creating two object models, User and Note, which you can work directly in the code. Also Entity Framework will provide the ability to save these models directly back to the database.
In the following steps, you will learn to add an Entity Framework item to the project and then connect to NotepadDB in SQL Azure and generate object models.
Right-click the NotepadServiceRole project found in Solution Explorer, and choose Add => New Item.
Click the Data from Installed Templates list, choose ADO.NET Entity Data Model, and name the model NotepadService.edmx, as shown in Figure 3.
Figure 3. Adding Entity Framework
You will be prompted with the Entity Data Model Wizard, as shown in Figure 4. Click the Next button.
Figure 4. Entity Data Model Wizard
Click the "New Connection..." button, and when Choose Data Source appears, select Microsoft SQL Server from the list, as shown in Figure 5. Click the Continue button.
Figure 5. Choose Data Source window
You will be prompted with a Connection Properties window. In the service name, put the SQL Azure server name that you acquired from the previous steps and enter "NotepadAdmin" and "P@ssword" as your username and password. Then from the "Select or enter database name" drop-down, select NotepadDB, as shown in Figure 6.
Figure 6. Connection Properties window
Click the OK button, and you will return to the Entity Data Model Wizard window. Select Yes, include the sensitive data in the connection string radio button, and click the Next button.
If you expand the tables, you will see the two tables (Note and User) that you created previously. Select both of the tables, as shown in Figure 7.
Figure 7. Choosing the database objects
Take the default option for everything else, click the Finish button, and you will return to the Visual Studio project and see Notepad.edmx, which contains two object models: User and Note, as shown in Figure 8.
Figure 8. Entity model Notepad.edmx
You now have User and Note object models that you can work with in your NotepadService. In the next section, you will be preparing NotepadService, which will implement simple create, read, update, and delete operations using the entity model that you generated in this section.