The Entity Framework is an object-relational mapping (ORM) tool that allows you to generate objects based on the tables in a database, taking care of the interaction to the database that otherwise you have to code yourself; the Entity Framework will save you lots time.
You will be creating a database in SQL Azure in order perform create, read, update, and delete operations for the Notepad application.
You will learn to create an SQL Azure account in the following steps.
Open a browser of your choice.
Go to www.microsoft.com/windowsazure/ to sign up and buy the Windows Azure service account. Follow the direction provided by Microsoft in order to purchase and acquire the service account in order to continue with the following steps. You can use Microsoft Azure each month for free (25 hours of computing time, 500 MB storage, 10,000 storage transactions, 1 GB database, and 500 MB data transfer); this promotional offer might end soon, though.
Go to http://sql.azure.com/ and sign in using the account you created in Step 1.
Once signed in, click the SQL Azure menu tab on the left side. When the Windows Azure page loads, you will see the project that you created during the registration process in Step 1. See Figure 1, which corresponds to this step.
After you register and purchase your Azure service account, you can now log in to an SQL Azure portal.
Click the project hyperlink NAS-DEV. In your case, you should click the name of the project that corresponds to the one you created in the "Signing up for SQL Azure" section.
Click the I Accept button on the Terms of Use page, as shown in Figure 2.
Here you will be creating an SQL Azure database with a username and password.
On the Create Server page, enter "NotepadAdmin" as the administrator username and "P@ssword" as the administrator password. Retype the password, and choose North Central US on the Location drop-down. See Figure 3 for the inputs. Note that for the location, you would want to choose the region where you are closest for optimal performance. If you are planning to deploy the application to a specific region, then you would want to select the appropriate region here.
Click the Create Server button and you will see Figure 4. Take note of the server name assigned to your SQL Azure database as you will need this information to connect using SQL Server Management Studio in Step 20.
Click Create Database. When the pop-up window appears, enter "NotepadDB" as the name of your database, choose "Web" on the "Specify an edition" drop-down menu, and choose "1GB" on the "Specify the max size" drop-down menu, as shown in Figure 5. Now click the Create button.
In the next steps, you will learn to properly configure the SQL Azure firewall so you can connect to the database. By default SQL Azure denies all access to the database until you add specific IP.
Notice that NotepadDB now appears in the list of databases on the Databases tab. Now click the "Firewall Settings" tab, where you will add your IP to the firewall in order to access the SQL Azure database that you just created from the Microsoft SQL Management Console application to perform various database-related tasks.
Select "Allow Microsoft Services to access to this server" check box, which will allow programs like Microsoft SQL Management consoles to connect directly to the SQL Azure database, as shown in Figure 6.
Click the Add Rule button, and when the popup appears, enter "MyComputer" into the Name text box. Notice that the popup displays your IP address, which you should now copy and paste into the IP Range text boxes, as shown in Figure 7.
Click the Submit button. Notice that firewall rules can take up to five minutes to go in effect.
In this section, you will learn to test to make sure all the configuration steps are performed properly and you can connect to the database.
Let's test to see if you can properly connect. Click the Databases tab.
From the list of databases, select NotepadDB, click the "Test Connectivity" button, and the pop-up window will appear, as shown in Figure 8.
Enter "NotepadAdmin" and "P@ssword" (or your own versions of these) into the Username and Password boxes and click the Connect button. You may choose to use your own username password of your choice.
Click the Connect button and you will see a "Successfully connected to the database" message.
Click the Close button to return to the main page behind.
In the following section, you will learn to create database tables in NotepadDB, which is hosted directly in SQL Azure using the Microsoft SQL Server Management application.
You will be connecting directly to the SQL Azure database NotepadDB you created in the foregoing steps using the SQL Management application.
You want to make sure that you can connect to SQL Azure directly from SQL Management Studio in order to perform various database operations. If you do not have SQL Management Studio installed, you can download the free SQL Server 2008 R2 Express (www.microsoft.com/express/database/). Open SQL Server Management Studio by going to Start => Programs => Microsoft SQL Server 2008 R2, as shown in Figure 9.
In the Connect to Server window, put the server name you obtained from Step 7 into the Server name text box, put NotepadAdmin and P@ssword into the Login and Password text box, and click the Connect button, as shown in Figure 10.
Once you are connected successfully to your SQL Azure database, SQL Server Management Studio will display an Object Explorer window on the left side of its IDE, as shown in Figure 3-14. Expand the Databases folder and you will find NotepadDB, which you just created, listed there, also shown in Figure 11.
Once you are connected to NotepadDB, you can create the tables you'll use to store and manage the notes your users will create and save. You will be creating the database schema shown in Figure 12.
Right-click NotepadDB from the Object Explorer window, and from the context menu choose New Query.
You will be executing SQL scripts in the query window in order to create tables in NotepadDB.
To the newly opened query window, enter or cut and paste the following database script.
USE [NotepadDB]GOCREATE TABLE [dbo].[User]( [UserId] [uniqueidentifier] NOT NULL, [Name] [nvarchar](50) NOT NULL, CONSTRAINT [PK_User] PRIMARY KEY ( [UserId] ))GoCREATE TABLE [dbo].[Note]( [NoteId] [int] IDENTITY(1,1) NOT NULL, [UserId] [uniqueidentifier] NOT NULL, [NoteText] [nvarchar](max) NOT NULL, [Description] [nvarchar](50) NOT NULL, CONSTRAINT [PK_Note] PRIMARY KEY CLUSTERED ( [NoteId] ))GOALTER TABLE [dbo].[Note] WITH CHECK ADD CONSTRAINT [FK_Note_User] FOREIGN KEY([UserId]) REFERENCES [dbo].[User] ([UserId])GOALTER TABLE [dbo].[Note] CHECK CONSTRAINT [FK_Note_User]GO
Notice that when you expand the tables from NotepadDB in Object Explorer, you will see two tables: Note and User, as shown in Figure 13.
Now you have the live database in SQL Azure ready to be used by your web service that you will be creating in next section. You will be creating a web service using Windows Communication Foundation (WCF). The web service layer provides managed APIs the phone application can use to access the database.