I like the idea of a database in the cloud. We have sort of been doing it for years, connecting to a SQL Server database over 1433. With SQL Azure, we take this one level further and let Azure worry about the hardware. So we don’t have to worry about scaling out with the bandwidth, RAIDs, etc.
Last week I showed the basics on how to migrate data from a SQL Server 2008 database to SQL Azure. Yesterday I showed using SQL Azure with Telerik OpenAccess and the WCF REST Toolkit. Today I will show how to build a simple REST based application using ADO.NET Data Services (aka Astoria.)
To get started we need a SQL Azure database. See my previous blog post about the CTP and getting data into your SQL Azure database. Once you have a SQL Azure database all set up let’s get to work.
The next thing we need to do is to create a new Web project and create our Entity Framework data model. I’ll go ahead and create an Entity Data Model against my local SQL Server 2008 Northwind database that has the same schema as my SQL Azure one. This is because SQL Azure and the Entity Framework chokes on the designer (or at least my version!) I will map:
Now that my EDM is all set up, I will go in and change the connection string in my web.config to use SQL Azure. Here is my new connection string:
provider connection string="Data Source=tcp:tpzlfbclx123.ctp.database.windows.net;
You have to manipulate the EF connection string and put in the SQL Azure server name of your CTP in the “Data Source” and put in the database name in the Initial Catalog, turn off integrated security and put in the UID/PWD from the CTP. I set MARS set to false since SQL Azure does not support MARS.
Now let’s create the Astoria Service. Add a new “ADO.NET Data Service” to your project. I named mine NwindRestService.
Astoria can’t make it any easier for you to get the service up and running. All you need to do is set up the name of your EDM in line 2, in our case it was NorthwindEntities and also set the access permissions on line 8. I just uncommented the generated line and put in an “*” so all of my entities will inherit the AllRead access rule. With that we are good to go!
1: //Enter the name of your EDM (NorthwindEntities)
2: public class NwindRestService : DataService<NorthwindEntities>
4: public static void InitializeService(IDataServiceConfiguration config)
6: //Must set up the AccessRule, here I allow read only access
7: //to all entities. I can also do this one by one.
8: config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
For the reality check, let’s run the service in the browser, being a RESTful service, Astoria will allow you to browse all of the Customers by typing in this URL:
We should see this:
I also edited my first row in Northwind (ALFKI) to say “SQL Azure” at the end of the customer name so I know I am working with the SQL Azure and did not mess up my connection strings. That is it, you now have a RESTful service that is hooked up to SQL Azure.
The hard part is over. Now let’s build a simple ASP.NET client to consume the RESTful data.
First you have to set a reference to your service. This will give you a proxy to write some LINQ (to Astoria) code against.
Next we will create a simple ASP.NET GridView control and bind some data to it on the page load event. (Sure we can do a lot more, but this is just to get our feet wet with SQL Azure.)
1: //the address of our service
2: Uri url = new Uri("http://localhost:1075/NwindRestService.svc/", UriKind.Absolute);
3: //a ref to our proxy
4: ServiceReference1.NorthwindEntities dat =
5: new ServiceReference1.NorthwindEntities(url);
7: //link statement to get the data, can use WHERE, Orderby, etc
8: var customers =
9: from c in dat.Customers
10: where c.Country == "Germany"
11: orderby c.CustomerID
12: select c;
14: //bind to the grid
15: GridView1.DataSource = customers;
This is pretty basic code from here. Line 2 is a Uri reference to our service (which is technically in the same project, but it could (and should) be in a different project.) Line 4-5 is setting up a reference to the proxy we created and this is also our data context, representing the Astoria service. Lines 8-12 is a simple LINQ to Astoria statement to filter by the German customers (look like LINQ to SQL? That is the point!) and Lines 15-16 is where we do the data binding to the ASP.NET GridView. Our gridview looks like this, notice the ALFKI record says it is coming from SQL Azure:
That is all there is too it. Enjoy.
Page rendered at Thursday, November 26, 2020 10:52:16 AM (Eastern Standard Time, UTC-05:00)
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in anyway.