Geeks With Blogs
Frank Wang's inspirations on .NET
IEnumerable<Inspiration> inspirations = from i in DataContext.Inspirations where i.Sharable == true select i

In my previous post, I gave you the introduction of ADO.NET Data Service. Now I will show you step by step how to create an ADO.NET data service using Visual Studio 2008 and the Entity Framework Tools (December 2007 CTP).

I am going to use the Northwind sample database to implement the service in this example. The Northwind database can be downloaded from: http://go.microsoft.com/fwlink/?linkid=24758.

1. Create an ASP.NET Web application (C#) in Visual Studio. Let's name the project NorthwindDataService

2. Create an Entity Data Model (EDM) based on your database using the ADO.NET Entity Framework. This is where the Entity Framework designer tools come into picture.

(1) Right-click the project name in Solution Explorer and select Add New Item, and choose ADO.NET Entity Data Model (this option will not show up if you don't have the Entity Framework Tools already installed). Name the data model Northwind. Northwind.edmx will be the generated file name. The schema of the EDM being created maps one-to-one with the Northwind database.

DataModel

(2) Click on Add to add the Entity Data Model. This will bring up the Entity Data Model Wizard.

Wizard

(3) Choose "Generate from database" and click on "Next". You will be prompted to choose the connection string for your Entity Data Model. Create a new connection for the Northwind database and click on "Next".

(4) The final step of the wizard discovers the Northwind database and lists all the database objects you want to include in your Entity Data Model.

DatabaseObjects

We will just leave all the objects checked and click on Finish. Congratulations you have just finished the creation the data model of your Data Services. So far we haven't written a single of line of code yet. You may be wondering what exactly is going on here. Essentially, the Entity Framework has created a complete data service based on the schema of the database. All the tables and views have been mapped to strongly-typed objects. The insert, update, delete methods for each one of the tables are also automatically created. You should notice a file named "Northwind.edmx" was added to the Solution Explorer after the Entity Data Model Wizard finished. Double click on this file and you will see the whole new world of Entity Framework.

ModelBrowser

What you are seeing now is the Entity Framework Tools. You can clearly see the database objects, the mapping between database to the strongly-typed objects, and the properties of these objects. This is the visual illustration of your data model. If you want to see what's really under the hood, go ahead and flip back to the Solution Explorer, expand the Northwind.edmx node and open the Northiwind.Designer.cs file.

CodeBehind

Don't get overwhelmed by this 3000 line CS file just yet. If you open your Class View and drill down to individual classes, you will understand that Entity Framework Tools generated all the proxy classes and put them under the NorthwindModel namespace.

3. Create the Data Service

Right-click the project name in Solution Explorer and select Add New Item. When the Add New Item window appears, select ADO.NET DataService. Name the service Northwind. Please note you must select ADO.NET Data Service, not other similarly-called options such as Web Service or WCF Service.

The Northwind.svc.cs has been generated for you with some instructional code. To enable the ADO.NET Data Service, we need to enable the resources in the EDM associated with the service. Modify the Northwind.svc.cs so it looks like this.

using System;
using System.Web;
using System.Collections.Generic;
using System.ServiceModel.Web;
using System.Linq;
using Microsoft.Data.Web;
using NorthwindModel;

namespace NorthwindDataService
{
    public class Northwind : WebDataService< NorthwindEntities>
    {
        public static void InitializeService(IWebDataServiceConfiguration config)
        {
            config.SetResourceContainerAccessRule("*", ResourceContainerRights.All);
        }
    }
}

4. Test the Northwind service.

As I mentioned in my previous blog (ADO.NET Entity Framework and Data Services in action (Part 1: Introduction), ADO.NET Data Service made the data available in the style of REST. So the easiest way to access the service is in a web browser. Ultimately, we will be building some client applications to consume the service, but for right now, using the web browser is good enough to see how the requests and responses look like.

Set the Northwind.svc to be the start page for your web application. Hit F5 or click Run to launch the web application.

Test1
The URL http://localhost:511155/Northwind.svc is the start point of the service. It lists all the entities exposed by the service. Using this as a starting point, you can drill down to the contents of the data service. For example, you can add /Products to the URL, resulting in http://localhost:511155/Northwind.svc/Products , which returns all of the products in the store, as partially shown below.

Products

The response also includes the relationships between entities. This can be really helpful if you want to browse more of the content. Let's take a look at the "Category" property of the "Product" 1.

http://localhost:51155/Northwind.svc/Products(1)/Categories

ProductCatogory

For more information about the HTTP syntax used by ADO.NET Data Services, see Simple Addressing Scheme for Data with Uniform URLs on the ASP.NET 3.5 Extension web site.

Next steps

We will be building a client application to consume our Northwind data service.

Posted on Saturday, March 8, 2008 9:24 PM ADO.NET Data Services , Entity Framework | Back to top


Comments on this post: ADO.NET Entity Framework and Data Services in action (Part 2: Creating ADO.NET Data Services)

# re: ADO.NET Entity Framework and Data Services in action (Part 2: Creating ADO.NET Data Services)
Requesting Gravatar...
not sufficent
Left by raju on Jul 28, 2009 7:56 AM

Your comment:
 (will show your gravatar)


Copyright © Frank Wang | Powered by: GeeksWithBlogs.net