Friday, July 30, 2010

Rob Sanders’ Working with Entity Framework v4 and SQL Azure: Parts 1 through 4

image Rob Sanders (@AusRob), who writes the Sanders Technology Blog, wrote a four-part blog series (and an addendum) about using SQL Azure to persist .NET entities with Entity Framework v4 in early 2010, just after SQL Azure was released for general use. He originally posted these articles to his blog from which they’re no longer available. I’m interested in this topic and decided to plug the linkage gap.

Following is a list of the four parts and addendum with links to posts on Rob’s new blog:

Update 8/1/2010: Rob posted the following tweet on 7/31/2010:

@rogerjenn Cheers for the plug :) I'm going to also investigate whether SQLAzure can be used with the new Code First approach (EFv4)

and I added Rob’s recent How To Choose a WCF Binding post and some background posts about use of EF4 with SQL Azure. See “Related Posts” below.

Following are the introductory section(s) of each post for reference:

Working with Entity Framework v4 and SQL Azure


Well, it’s been “out there” for a while, and now it is time I gave you a taste of how to combine some of the “Next Generation” data access technologies, today.  SQL Azure is Microsoft’s “Database in the Cloud” and represents a complete makeover (replacement) of the first attempt, SQL Data Services (SDS).  SDS is no longer, SQL Azure is the future.

One of the biggest knocks on SDS was the lack of a relational database.  Everything was done according to vertical data “silos” and cross-silo querying was painful (and almost impossible).  Unfortunately, administration was also tricky (well, worse) and it veered quite severely from the established SQL world which we know and love (or submit to).

Now, I’m going to walk you through the process of establishing a working Entity Framework (v4) model which runs against a SQL Azure database – a database in the “Cloud”.  This is not, by any means, a definitive guide.  In fact, I only just got this working myself this morning.  It relies on a few creative “workarounds”, which I will explain in due course.

This example is only intended as a “Proof of Concept” and has not exhaustively been tested. 
Attempt at your own peril (usual conditions and waivers apply)!


What you will need prior to following along:

All of the above can be obtained for free, from the links above.  You may have to wait for a SQL Azure invitation or token.  My tokens were obtained a little while ago.

I’ll consider writing a separate article on how to set up SQL Azure, so if you are interested send me an email or leave a comment here. …

Next Steps with SQL Azure


So now that we’ve had a look at some of the basics around SQL Azure, Microsoft’s Database in the cloud, let us take a look at something more practical.  For our next example, I’m going to refer to a schema I designed and populated last year – my vinyl record collection.  You can have a look at the details of the schema in a previous entry – Project Sneak Peek.

The project was previously defined in this entry – New Project Announcement: SqlAzure Database and Azure Application (though we may skip the Azure application part, depending on time).

Given the schema is for SQL Server 2008, one of our first tasks will be to script the database and existing data, and then convert it to be conform with SQL Azure’s T-SQL restrictions. …

Putting it all together – SQL Azure

Now we should have a working data model which has been created in your SQL Azure (Cloud) database.  Taking lessons learned from Part 2, we’re going to create an Entity Framework (v4) model of the SQL Azure tables using Visual Studio 2010 Beta 2.

This entry is sort of a rehash of the first and second entries, but the outcome should provide you with a fully functioning Entity Framework (v4) data model, and a Unit Test to prove it works! …

A Dynamic Data Website with SQL Azure and the Entity Framework

Continuing along…

Our next step is to create a Dynamic Data website.  If you haven’t come across this yet, it’s most likely because you haven’t been using Visual Studio 2010 or the .Net Framework 4.0.  Recently introduced and compatible with both LINQ-to-SQL and the Entity Framework, this nice site template makes use of the dynamic nature of both LINQ-to-SQL [.dbml] (SqlMetal) and Entity Framework [.edmx] data models.

Continuing within the solution we created in Part 3, we shall now add a Dynamic Data website to our solution.  Open the solution in Visual Studio 2010 Beta 2 and then Right click the solution, “Add –> New Project..”.

Please note: this portion of the solution will also work in conjunction with any other database provider supported by the Entity Framework, not just SQL Azure.  To use a Dynamic Data site all you need is a LINQ-To-SQL or Entity Framework Data Context!  For details, read on. ...

Getting a SQL Azure Database


This article will describe the steps you need to take to obtain a SQL Azure database.  The main website for SQL Azure is and for developers

The Microsoft Connect site ( ) is now closed, but you may still find useful information here, such as links to the team blog.  There are SQL Azure forums for community discussions located here.

To get started with SQL Azure, you must have a valid Windows Live ID.  You can sign up for a Live ID with a valid email address by visiting

You’ll need to create an account with Microsoft Online Services at
Sign in with a valid Windows Live ID, and then you will be prompted for some profile information. …

Parts of the addendum apply only to residents of Australia. According to his new Sanders Technology Blog, Rob now lives in HangZhou, China.

Related posts:

blog comments powered by Disqus