October 26, 2016
Hot Topics:

Creating a Windows Azure App: Step 2 - Create the Model, Page 2

  • January 18, 2012
  • By Michael Collier
  • Send Email »
  • More Articles »

Step 2 – Create the Model

For this project, a code first approach will be used with the Entity Framework for working with the data model. With this approach, the data model (i.e. entity classes) is created prior to a database or tables being created. Create a simple POCO (Plain Old CLR Objects) that will serve as the entity classes to work with. The Entity Framework will automatically create the necessary SQL Azure database and tables that relate to the defined POCO entity classes. To learn more about Entity Framework or code first with Entity Framework, please visit http://msdn.microsoft.com/en-us/data/aa937723.

While Entity Framework is used for this project, virtually any data access approach used to access SQL Server can also be used for working with SQL Azure. This could be ADO.NET, NHibernate, or any other data access technology you're fond of. SQL Azure uses TDS (Tabular Data Stream) as its underlying protocol. This is the same protocol that SQL Server uses. It should be noted that SQL Azure is at this time not 100% feature compatible with SQL Server. Please visit http://msdn.microsoft.com/en-us/library/ff394115(v=MSDN.10).aspx for details on incompatible features.

First, create two new models – one to model a golf course, and the other to model a round at a course. These new model classes will be placed in the Models folder of the project.

	public class Course
		public int CourseId { get; set; }
		public string Name { get; set; }
		public string Url { get; set; }
		public string AddressLine1 { get; set; }
		public string AddressLine2 { get; set; }
		public string City { get; set; }
		public string State { get; set; }
		public string ZipCode { get; set; }
	public class Round
		public int RoundId { get; set; }
		public int CourseId { get; set; }

		public DateTime RoundDate { get; set; }
		public int FinalScore { get; set; }
		public int ScoreIn { get; set; }
		public int ScoreOut { get; set; }
		public int GreensInRegulation { get; set; }
		public int Puts { get; set; }

		public virtual Course Course { get; set; }

Now that the models are in place, we need to associate them with SQL Azure tables for persistence. This can be accomplished by using a database context class, DbContext. Create a new class, GolfContext, which simply derives from DbContext (in the System.Data.Entity namespace). The new GolfContext class will have two properties that will be used to work with the data. The GolfContext class should look like the code below.

	public class GolfContext : DbContext
		public DbSet<Course> Courses { get; set; }
		public DbSet<Round> Rounds { get; set; }

By default, the Entity Framework looks for a database connection string in the Web.config file that matches that name of the database context class. In this scenario, that would be GolfContext. The connection string is in the following format:

Data Source=tcp:YOUR-DATABASE-HERE.database.windows.net,1433; Database=GolfRounds; 
            User ID=YOUR-USERNAME@YOUR-SERVER; Password=YOUR-PASSWORD; Trusted_Connection=False; Encrypt=True;"

The connection string should look like the image below once complete.

Database Connection String v2

Notice that SQL logins are being used. This is because SQL Azure currently supports SQL logins only. You can leave these as placeholders for now. A SQL Azure account will be created in later steps.

Step 3 – Add the Controllers

Create a couple of controllers to work with our Course and Round entities. Now would be a good time to build the solution to make sure there are no errors. Building the solution at this point is also necessary for Visual Studio to be able to pick up the model classes that will be used in the controllers that will be created.

Create a new controller for both the Course and the Round models. To do that, right click on the “Controllers” folder in the ASP.NET MVC3 project, select “Add”, and then “Controller”. This will launch a dialog that will set up the scaffolding for the new controller. Select one of the model classes (Course or Round), and also the data context class to use (GolfContext). This should be done for both a CourseController and a RoundController.

Add Controller - Course - MyGolfStory

This process will automatically create a lot of baseline code in the project. The wizard will automatically generate the basic Create, Insert, Update, and Delete methods in the controller (notice the Template option in the wizard) as well as the respective views.


New Course and Round Views - MyGolfStory

The solution should build successfully at this point as well.

Step 4 – Sign up for a Windows Azure Account

A Windows Azure account is needed in order to use SQL Azure. Details on signing up for free 90-day trial of Windows Azure can be found at https://www.windowsazure.com/en-us/pricing/free-trial/. If you have an MSDN subscription, Windows Azure usage is included and you can use that as well. Remember, the Microsoft Platform Ready team can help with any questions related to creating a new Windows Azure Account. For help, please visit http://www.microsoftplatformready.com.

Step 5 – Create the SQL Azure Database

While Entity Framework will create the necessary database and tables, it will not create the SQL Azure server. In order to create the SQL Azure server, sign in to the Windows Azure Management Portal at http://windows.azure.com, doing so with the Windows Live ID associated with a valid Windows Azure subscription.

The first thing to do is create a new SQL Azure server.

  1. In the panel on the left side, click on “Database”.
  2. In the left panel, expand the “Subscriptions” folder if necessary to see the available Windows Azure subscriptions.
  3. Highlight the Windows Azure subscription that the SQL Azure server should be associated with. The SQL Azure servers will be listed below the subscription, as can be seen in the image below.

    New SQL Azure Server - Steps 1 - 3

  4. Click the “Create” button in the Server section of the top ribbon.
  5. Select the Region where the SQL Azure server should be located. This will be one of the six available Windows Azure data centers.
  6. Create a new SQL Azure administrative login and password. This username and password is like the SQL Server 'sa' account. You can create application specific logins, but for this walkthrough, just use the administrative account.
  7. By default, all communication with SQL Azure is blocked by a firewall. In order to connect to SQL Azure, firewall rules need to be enabled to allow external services to connect, as well as services running inside a Windows Azure data center. To add a new firewall rule, click the "Add" button on the "Create Server" dialogue to launch a new window that will allow you to enter the IP address to be added to the firewall rule list.

    SQL Azure - Firewall Rules - Blur

  8. At this point the SQL Azure server should be created and listed under the subscription selected earlier.

With the SQL Azure server created, add the necessary information to the Web.config file. Earlier a “GolfContext” connection string was created in the Web.config file. The placeholders there should now be replaced with the actual values for the SQL Azure server, the SQL Azure user ID (format of userID@server), and the password.

At this point proceed with launching the site. Simply press F5 from within Visual Studio to launch the site in Debug mode. If everything was done correctly to this point, the site should launch without any errors. No SQL Azure database has been created yet, because no Entity Framework code has been invoked. To do so, click either the "Courses" or "Rounds" navigation menu tabs at the top of the page. The page may take a few seconds to load – that’s Entity Framework creating the desired SQL Azure database and tables.

At this point proceed to the Windows Azure Management Portal to see the “GolfRounds” database created on the specified SQL Azure server. To view the tables, launch the SQL Azure Management Portal by selecting “Manage” from the “Database” section in the top ribbon. The SQL Azure Management Portal allows for viewing SQL Azure tables (such as the new Courses and Rounds tables), viewing dependencies, viewing execution plans, and other general database management features.

SQL Azure Management Portal v2

To make sure it works, feel free to try to add a new Course and then a new Round.

Tags: cloud computing, Azure, cloud development, Azure Cloud, Microsoft Platform Ready

Page 2 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel