Creating the Data Service (ADO.NET Data Services Quickstart)

In this task, you will create a sample data service that is based on the Northwind sample database. The task involves the following basic steps:

  1. Create an ASP.NET Web application.

  2. Define the data model by using the Entity Data Model tools.

  3. Add the data service to the Web application.

  4. Enable access to the data service.

To create the ASP.NET Web application

  1. In Visual Studio, on the File menu, select New, and then select Project.

  2. In the New Project dialog box, select either Visual Basic or Visual C# as the programming language.

  3. In the Templates pane, select ASP.NET Web Application. Note: If you use Visual Studio Web Developer, you must create a new Web site instead of a new Web application.

  4. Type NorthwindService as the name of the project.

  5. Click OK.

  6. (Optional) Specify a specific port number for your Web application. Note: the port number 12345 is used in a majority of the ADO.NET Data Services code examples.

    1. In Solution Explorer, right-click the name of the ASP.NET project that you just created, and then click Properties.

    2. Select the Web tab, and set the value of the Specific port text box to 12345.

To define the data model

  1. In Solution Explorer, right-click the name of the ASP.NET project, and then click Add New Item.

  2. In the Add New Item dialog box, select ADO.NET Entity Data Model.

  3. For the name of the data model, type Northwind.edmx.

  4. In the Entity Data Model Wizard, select Generate from Database, and then click Next.

  5. 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.

  6. 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.

  7. Click Finish to close the wizard.

To create the data service

  1. In Solution Explorer, right-click the name of your ASP.NET project, and then click Add New Item.

  2. In the Add New Item dialog box, select ADO.NET Data Service.

  3. For the name of the service, type Northwind.

    Visual 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.

  4. 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)

To enable access to the data service

  • In the code for the data service, replace the placeholder code in the InitializeService function with the following:

    ' Make certain entity sets writable.
    config.SetEntitySetAccessRule("Customers", EntitySetRights.All)
    config.SetEntitySetAccessRule("Employees", EntitySetRights.All)
    config.SetEntitySetAccessRule("Orders", EntitySetRights.All)
    config.SetEntitySetAccessRule("Order_Details", EntitySetRights.All)
    config.SetEntitySetAccessRule("Products", EntitySetRights.All)
    ' Make the remaining entity sets read-only.
    config.SetEntitySetAccessRule("*", EntitySetRights.AllRead)

    This enables authorized clients access to resources for the specified entity sets.

    Note Note

    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 ASP.NET Web Sites.

You have successfully created a new data service that is based on the Northwind sample database, and you have enabled access to the service for clients that have permissions on the ASP.NET Web application. Next, you will start the data service from Visual Studio and you will access the data service by submitting HTTP GET requests through a Web browser to the exposed resources:

Accessing the Service from a Web Browser

Community Additions