You are on page 1of 18


OData and WCF Data Services

This week we started investigating OData and WCF data services as a way to easily expose and query a remote
OData, the Open Data Protocol is a protocol for querying and updating data over using Http and AtomPub
Visual studio 2010 and .NET 4.0 come with rich support to both produce and consume data using the Open Data
This post is a tutorial on how to set up a web service that exposes OData, and how to query it from a client

• Download and install the Northwind database
• Visual Studio 2010
• .NET 4.0 Framework or the Data Services update for Microsoft .NET Framework 3.5 Service Pack 1

Exposing data as Odata

Create a new ASP.NET Web application

Create the Entity Data Model for the Northwind database

The Entity Data Model will be the base for the WCF Data Service that will expose the Northwind data in the OData
Here we can choose which Tables, Views and Stored Procedures will be exposed
Finish creates the Entity Data Model for the Northwind database
Create a new WCF Data Service
The NorthwindService inherits from the DataService with as generic parameter the EntityDataModel we want to
expose, in this case the NorthwindEntities we just created.
By default noone can do anything, we first need to set access rules for our entities.
The easyest way to do this is to expose all entities with read-all rights:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
If the Entity Data Model is in a different assembly you’ll need to add a reference, and make sure the connectionstring
is in the web.config of the referencing project

Visiting the service now shows the exposed data:

Consuming the data
To use the data exposed by the web service we create a client application.
Add a Service Reference to the web service.
For testing, there is always a public Northwind web service available
This generates a web service proxy.
New is that this also generates a DataServiceContext.
Using the DataServiceContext we can query the data service through a linq provider.
The following extremely simple example print a list with the Name and Unit Price of all available products.

class Program
static void Main(string[] args)
string url = "http://localhost:40171/NorthwindService.svc/";
var context = new NorthwindEntities(new Uri(url));

var someProducts = from p in context.Products

where !p.Discontinued
select p;

foreach (var product in someProducts)

Console.Out.WriteLine("{0} -
Download the sample code:

More information, samples, articles and tutorials can be found on

Some more links to get you started:
• Open Data Protocol Visualizer
• Open Data Protocol (OData) with Pablo Castro
• Episode 43: Talking OData and SQL Modeling with Douglas Purdy
• Carl and Richard get the word on oData from Brad Abrams, Bob Dimpsey and Lance Olson.
• Using Data Services over SharePoint 2010 – Part 1 – Getting Started
• The Open Data Protocol (OData),guid,28df55a8-8811-4c4b-b319-75c5c58d1444.aspx

Posted by Mel Gerats on 9-Jun-10

2 Comments | Trackback Url | Bookmark this post with:

Tom Pester commented on Wednesday, 9-Jun-2010
Some more links to get you started : Open Data Protocol Visualizer
us/f4ac856a-796e-4d78-9a3d-0120d8137722 <Open Data Protocol (OData) with Pablo Castro/> Episode 43: Talking OData and SQL Modeling with
Douglas Purdy
%28Deep+Fried+Bytes%29&utm_content=Google+Reader Carl and Richard get the word on oData from Brad
Abrams, Bob Dimpsey and Lance Olson. Using Data
Services over SharePoint 2010 – Part 1 – Getting Started
started.aspx The Open Data Protocol (OData),guid,28df55a8-8811-4c4b-

Tom Pester commented on Wednesday, 9-Jun-2010

first paragraph: test from IE8 first paragraph: test from IE8first paragraph: test from IE8 first paragraph: test from IE8
second paragraph : test from IE8 second paragraph : test from IE8 second paragraph : test from IE8 second
paragraph : test from IE8

Your name *
Email *
Your URL

Comment *
Please enter the text from the image

The Northwind and Pubs sample databases are staples of other sample code on this and
other development websites. They are used in countless tutorials and walkthroughs
published by Microsoft and others. Being a SQL/SQL Server newbie myself, I figured there
may be others in my boat who might want both databases in an easily-accessible spot, and
an easier, less error-prone way of installing them into SQL Server 2005 Express Edition
than, say, running SQL query scripts.

It's lost on me why one must use *.sql query scripts to install these samples. It's also lost
on me why SQL Server distributions don't come with these databases provided, since they
are used so ubiquitously through documentation, websites, and tutorials. There are probably
several good reasons for this. However, I am focused on usability, and not on making things
harder than they have to be.
There undoubtedly will be readers who will protest and say that I should use *.sql query
files whenever possible; this is good and a perfectly acceptable practice. I see this article as
illustrating just another way to accomplish a similar objective.
It's worth noting, for example, that a default Visual Studio .NET 2005 Standard Edition
installation does not distribute these databases. It goes part-way, creating a Program
Files\Microsoft Visual Studio 8\SDK\Samples\Setup directory containing, e.g., instnwnd.sql,
a query script that ostensibly can be used to install the Northwind sample database.
However, hats off to Microsoft for putting apples with apples. Microsoft provides
the queries for installing, but not the *.mdf and *.ldf files for thedatabases themselves.
This does not support users in installing these important samples into SQL Server. Perhaps
there's an easier way: this article. The download attached to this article contains the SQL
Server 2000 versions of these files.
Personally, I favor using graphical (GUI) tools and methods over typing cryptic commands
and deciphering error messages. So, this article illustrates how to install Northwind and
Pubs using graphical tools and methods.

Steps: Installing the Northwind and Pubs Databases

Note: The steps here make use of SQL Server Management Studio Express, which
sometimes doesn't install along with a Microsoft SQL Server 2005 Express Edition install.
For this case, SQL Server Management Studio Express is available as a separate download
from Microsoft.
Step 1: Using the download link above, download the Zip file containing the following files.
Extract the files to a directory on your computer and remember where you put them. For
these steps to work, you must have the following files on your computer:
Step 2: On the Start menu, point to Programs, point to Microsoft SQL Server 2005,
and then click SQL Server Management Studio Express.
SQL Server Management Studio starts and displays the Connect to Server window, shown
in Figure 1.

Figure 1. The Connect to Server window

Fill in the appropriate settings on the Connect to Server window that appears, and then
click Connect.
SQL Server Management Studio Express opens the Object Explorer with the server's
databases displayed.
Step 3: Right-click the Databases folder and then click Attach, as shown in Figure 2.
Figure 2. The Object Explorer window

Step 4: In the Attach Databases window, click Add....

The system prompts you for the proper *.mdf file using the Locate Database File window,
as shown below in Figure 3.
Figure 3. The Locate Database File window

Step 5: Locate and click on the NORTHWND.MDF file, and then click OK.
The right pane of the Attach Databases window will look as shown in Figure 4.
Figure 4. The right pane of the Attach Databases window after adding the Northwind database

Step 6: Repeat steps 4 and 5 for the PUBS.MDF file.

When you're done, and if you did the proper actions, the right pane of the Attach
Databases window should look as displayed in Figure 5.
Figure 5. The right pane of the Attach Databases window after adding the Pubs database

Step 7: In the Attach Databases window, click OK.

Step 8: To check that everything was successful, in the Object Explorer window, click the
plus sign to open the Databases folder.
The Northwind and Pubs databases will be displayed, as shown in Figure 6.
Figure 6. The Object Explorer after importing the Northwind and Pubs databases

Where to Go for Help

Feel free to email me with specific questions about this article, and I will be happy to
explain. I also want to invite you to make use of the forums at the bottom of this article. I
wish you well and happy programming. Click here for the SQL Server 2008 version of this

In this section, I will keep a running history of the changes and updates I've made to this
• 29 Sept. 2009, 11:25 PM Central: Article download link to reflect the official
Microsoft release location
• 16 Oct. 2007, 2:13 PM Pacific: Article written.

This article, along with any associated source code and files, is licensed under The Code
Project Open License (CPOL)

About the Author

Brian C. Hart, Ph.D. From Fridley, Minnesota and I like computer programming! When I
got started, I was working mostly with Windows GUI programming in
C/C++. Then later on I worked with COM/DCOM for a school
internship. I used COM/DCOM to write an ad hoc cluster server and
job-running environment for a cluster of 24 Windows-based high-end
visualization workstations. I moved on to C# and have been working
in C# and Windows Forms ever since. I have yet to embrace


Urban Science

United States