Creating the Northwind Data Service (WCF Data Services Quickstart)
In this task, you will create a local version of the Northwind sample data service that uses WCF Data Services to expose an Open Data Protocol (OData) feed that is based on the Northwind sample database. This version will support updating data in the Northwind database by using OData. The task involves the following basic steps:
Create an ASP.NET Web application.
Define the data model by using the Entity Data Model tools.
Add the data service to the Web application.
Enable access to the data service.
The ASP.NET Web application that you create when you complete this task runs on the ASP.NET Development Server provided by Visual Studio. ASP.NET Development Server only supports access from the local computer. To also make it easier to test and troubleshoot the data service during development, consider running the application that hosts the data service by using Internet Information Services (IIS). For more information, see How to: Develop a WCF Data Service Running on IIS.
To create the ASP.NET Web application
In Visual Studio, on the File menu, select New, and then select Project.
In the New Project dialog box, under either Visual Basic or Visual C# select the Web template, and then select ASP.NET Web Application.
If you use Visual Studio Web Developer, you must create a new Web site instead of a new Web application.
Type NorthwindService as the name of the project.
(Optional) Specify a specific port number for your Web application. Note: the port number 12345 is used in the rest of the quickstart.
In Solution Explorer, right-click the name of the ASP.NET project that you just created, and then click Properties.
Select the Web tab, and set the value of the Specific port text box to 12345.
To define the data model
In Solution Explorer, right-click the name of the ASP.NET project, and then click Add New Item.
In the Add New Item dialog box, click the Data template and then select ADO.NET Entity Data Model.
For the name of the data model, type Northwind.edmx.
In the Entity Data Model Wizard, select Generate from Database, and then click Next.
Connect the data model to the database by doing one of the following steps, and then click Next:
If you do not have a database connection already configured, click New Connection and create a new connection. For more information, see How to: Create Connections to SQL Server Databases. This SQL Server instance must have the Northwind sample database attached.
- or -
If you have a database connection already configured to connect to the Northwind database, select that connection from the list of connections.
On the final page of the wizard, select the check boxes for all tables in the database, and clear the check boxes for views and stored procedures.
Click Finish to close the wizard.
This generated data model exposes foreign key properties on entity types. Data models created using Visual Studio 2008 do not include these foreign key properties. Because of this, you must update the client data service classes of any client applications that were created to access the Northwind data service that was created using Visual Studio 2008 before attempting to access this version of the Northwind data service.
To create the data service
In Solution Explorer, right-click the name of your ASP.NET project, and then click Add New Item.
In the Add New Item dialog box, select WCF Data Service.
For the name of the service, type Northwind.
Visual StudioVisual Studio creates the XML markup and code files for the new service. By default, the code-editor window opens. In Solution Explorer, the service will have the name, Northwind, with the extension .svc.cs or .svc.vb.
In the code for the data service, replace the comment /* TODO: put your data source class name here */ in the definition of the class that defines the data service with the type that is the entity container of the data model, which in this case is NorthwindEntities. The class definition should look this the following:
Public Class Northwind Inherits DataService(Of NorthwindEntities)
public class Northwind : DataService<NorthwindEntities>
To enable access to data service resources
In the code for the data service, replace the placeholder code in the InitializeService function with the following:
' Grant only the rights needed to support the client application. config.SetEntitySetAccessRule("Orders", EntitySetRights.AllRead _ Or EntitySetRights.WriteMerge _ Or EntitySetRights.WriteReplace) config.SetEntitySetAccessRule("Order_Details", EntitySetRights.AllRead _ Or EntitySetRights.AllWrite) config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead)
// Grant only the rights needed to support the client application. config.SetEntitySetAccessRule("Orders", EntitySetRights.AllRead | EntitySetRights.WriteMerge | EntitySetRights.WriteReplace); config.SetEntitySetAccessRule("Order_Details", EntitySetRights.AllRead | EntitySetRights.AllWrite); config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead);
This enables authorized clients to have read and write access to resources for the specified entity sets.
Any client that can access the ASP.NET application can also access the resources exposed by the data service. In a production data service, to prevent unauthorized access to resources you should also secure the application itself. For more information, see Securing WCF Data Services.
You have successfully created a local, writable version of an OData service based on the Northwind sample database. You have also enabled access to the feed for clients that have permissions on the ASP.NET Web application. Next, you will create a second client application that binds this Northwind data to Windows controls and send updates to this new writable OData service: