You have heard a lot about the ADO.NET Entity Framework over the last few weeks. What you may not have heard about is just how easy the EF makes it to expose your data as a cloud service in ATOM format over the REST protocol. I'll show you just how easy it is to do here.
First open Visual Studio 2008 with SP 1 installed. The EF and "Astoria" (or ADO .NET Data Services) both ship with SP1.
Choose File|New Project and choose ASP .NET 3.5. Name the project NorthwindTestService and hit ok.
Go ahead and delete the Default.aspx file and right click on the project and click on "Add->New Item". Highlight "ADO.NET Entity Data Model" and name your model Northwind (or something similar) and click ADD.
This brings you to the Entity Framework wizard. Choose to work from an existing database and put in the connection for Northwind (or create it if it does not exist already) and click through the wizard until it brings you to the Choose Your Database Objects dialog. Choose Customers, Orders, and OrderDetails and click finish.
OO experts will tell you to rename your objects (to be singular, not plural for starters), but let's leave them the same for now. Next step is to right click on the project and select Add->New Item and select "ADO .NET Data Service."
Give your service a name of NorthwindTestsercvice or something like that. Then Click ADD.
The NorthwindTestService.svc,cs file should be open and you have to make two modifications to get your data exposed. There are tons of modifications you can make around permissions and what data to expose, but for now let's just get up and running and expose everything. You need to name your DataService, give it the name of your Entity Diagram that you build in the wizard, in our case it is NorthwindEntities. (By default the wizard names it DatabasenameEntities.) Then add one line of code to set the access for everything in your model ("*") and giving it read access. (If you make this .All then users can read and write.)
public class NorthwindTestService : DataService< NorthwindEntities>
// This method is called only once to initialize service-wide policies.
public static void InitializeService(IDataServiceConfiguration config)
Now hit F5. (You may have to turn off IE 6/7 or FireFox's Auto RSS reader off)
The URL should be something like this (maybe a different port):
And you should see XML in ATOM format like this:
- <collection href="Customers">
- <collection href="Order_Details">
- <collection href="Orders">
Now the fun starts. Let's look at the customers:
You will see a list in ATOM format of all the customers in your Customers table. Now look at just one customer:
Now all the orders for that customer (careful it is case sensitive):
Now one order:
You get the idea. You can even filter inside of the URI with some rudimentary WHERE clauses.