2.8.3. Creating a Data Service using the ADO.NET Entity Framework

ADO.NET Data Services are a specialized form of Windows Communication Foundation services, and thus can be hosted in various environments. The below example will create an ADO.NET Data Service which is hosted inside an ASP.NET site. In order to create a data service, you must first create a web project; you will then need to establish a connection with the database that will be exposed by the service, and then create the data service itself within the web application. Below is a step-by-step description of this process.

The following steps can be used for creating a Data Service using the Virtuoso ADO.Net Provider for accessing the sample Northwind Demo database:

  1. Launch the Visual Studio 2008 SP1 IDE.

    Figure 2.117. Visual Studio 2008 SP1 IDE.

    Visual Studio 2008 SP1 IDE.

  2. Create a

    Web Application

    project by going to the


    menu in Visual Studio and choosing

    New Project


  3. When the New Project window appears, choose either

    Visual Basic


    Visual C#

    as the programming language.

  4. Within the language category click on


    , and select

    ASP.NET Web Application

    from the right-hand panel.

  5. Choose a name for the project, for example


    , and click



    Figure 2.118. Name project

    Name project

  6. This will create a new project called



    Figure 2.119. Create project

    Create project

  7. Right click on the


    project name of the Solution Explorer pane, then select the

    Add -> New Item

    menu options.

    Figure 2.120. New Item

    New Item

  8. The


    New Item dialog will appear, choose the

    ADO.NET Entity Data Model

    template, give it the name


    and click


    to start the creation of the ADO.Net Entity Data Model.

    Figure 2.121. Entity Model

    Entity Model

  9. In the

    Entity Data Model Wizard


    Choose Model Contents

    page select the

    Generate from Database

    model type and click



    Figure 2.122. Model Contents

    Model Contents

  10. In the

    Entity Data Model Wizard


    Choose your Data Connection

    page select the

    New Connection


    Figure 2.123. Data Connection

    Data Connection

  11. In the

    Choose Data Source

    dialog, select the OpenLink

    Virtuoso Data Source

    from the list displayed and click



    Figure 2.124. Data Source

    Data Source

  12. In the

    Add Connection

    dialog, specify the

    hostname, portno, username and password

    for the target Virtuoso Server and check the

    Save Password

    check box.

    Figure 2.125. Connection Properties

    Connection Properties

  13. Select the

    Select Database From List

    radio button and choose


    from the drop down list, assuming the Virtuoso Demo Database is installed.

    Figure 2.126. Advanced Properties

    Advanced Properties

  14. Click the

    Test Connection

    button to verify the connection is successful and then click OK to add the connection.

    Figure 2.127. Test Connection

    Test Connection

  15. Set the

    entity connect string

    name to


    (note this name as it is required in step 17 below) and click



    Figure 2.128. entity connect string

    entity connect string

  16. In the

    Choose your Database Objects

    page select the


    check box to select all tables in the Demo database for addition to the Entity Data Model, set the

    Model Namespace



    and click



    Figure 2.129. Database Objects

    Database Objects

  17. The


    EDM will be created with the tables and relationships displayed in the Visual Studio IDE

    Figure 2.130. Virtuoso.edmx


  18. Right click on the


    project name of the

    Solution Explorer pane

    , then select the

    Add -> New Item

    menu options.

    Figure 2.131. New Item

    New Item

  19. The

    Add New Item

    dialog will appear, choose the

    ADO.NET Data Service

    template, give it the name


    and click


    to create the ADO.Net Data Service.

    Figure 2.132. ADO.NET Data Service

    ADO.NET Data Service

  20. In the


    Data Service file created add the data source class name of


    (note this is the name set in step 12) as the


    name and enable the access to the Data Service by adding the entry

    config.SetEntitySetAccessRule("*", EntitySetRights.All);

    in the



    // C#
    using System;
    using System.Web;
    using System.Collections.Generic;
    using System.ServiceModel.Web;
    using System.Linq;
    using System.Data.Services;
    namespace SimpleDataService
        public class Northwind : DataService<VirtuosoDemoEntities>
            public static void InitializeService(IDataServiceConfiguration  config)
                config.SetEntitySetAccessRule("*", EntitySetRights.All);

    Figure 2.133. Data Service

    Data Service

  21. To test the Data Service, simply hit


    within Visual Studio, which will start the development web server, run the Data Services server inside and load a Web browser page displaying the list of available tables/entities of the Demo database.

    Figure 2.134. test the Data Service

    test the Data Service

  22. To access a specific entity instance like the




    record, this would be specified as http://host/vdir/Virtuoso.svc/Customers('ALFKI') .

    Figure 2.135. Access a specific entity instance

    Access a specific entity instance


  1. Important

    - To view


    (the default format returned by an ADO.NET Data Service) in Internet Explorer, you must first ensure that

    Feed Reading View

    is turned


    . This can be done on the

    Content tab


    Tools in Internet Options


  2. If a Data Services entity instance URI page fails to load you can turn


    errors on by adding

    config.UseVerboseErrors = true;

    in the

    virtuoso.svc.cs InitializeService

    method to obtain more detailed information from the server as to why the page failed to load:

    public static void InitializeService(IDataServiceConfiguration config)
    config.UseVerboseErrors = true;
    config.SetEntitySetAccessRule("*", EntitySetRights.All);