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.

    Figure2.117.Visual Studio 2008 SP1 IDE.

    Visual Studio 2008 SP1 IDE.

  2. Create a

    Web Application

    project by going to the

    File

    menu in Visual Studio and choosing

    New Project

    .

  3. When the New Project window appears, choose either

    Visual Basic

    or

    Visual C#

    as the programming language.

  4. Within the language category click on

    Web

    , and select

    ASP.NET Web Application

    from the right-hand panel.

  5. Choose a name for the project, for example

    VirtuosoDataService

    , and click

    OK

    .

    Figure2.118.Name project

    Name project

  6. This will create a new project called

    VirtuosoDataService

    .

    Figure2.119.Create project

    Create project

  7. Right click on the

    VirtuosoDataService

    project name of the Solution Explorer pane, then select the

    Add -> New Item

    menu options.

    Figure2.120.New Item

    New Item

  8. The

    Add

    New Item dialog will appear, choose the

    ADO.NET Entity Data Model

    template, give it the name

    Virtuoso.edmx

    and click

    Add

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

    Figure2.121.Entity Model

    Entity Model

  9. In the

    Entity Data Model Wizard

    dialog

    Choose Model Contents

    page select the

    Generate from Database

    model type and click

    Next

    .

    Figure2.122.Model Contents

    Model Contents

  10. In the

    Entity Data Model Wizard

    dialog

    Choose your Data Connection

    page select the

    New Connection

    button

    Figure2.123.Data Connection

    Data Connection

  11. In the

    Choose Data Source

    dialog, select the OpenLink

    Virtuoso Data Source

    from the list displayed and click

    Continue

    .

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

    Figure2.125.Connection Properties

    Connection Properties

  13. Select the

    Select Database From List

    radio button and choose

    Demo

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

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

    Figure2.127.Test Connection

    Test Connection

  15. Set the

    entity connect string

    name to

    VirtuosoDemoEntities

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

    Next

    .

    Figure2.128.entity connect string

    entity connect string

  16. In the

    Choose your Database Objects

    page select the

    Tables

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

    Model Namespace

    to

    VirtuosoDemoModel

    and click

    Finish

    .

    Figure2.129.Database Objects

    Database Objects

  17. The

    Virtuoso.edmx

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

    Figure2.130.Virtuoso.edmx

    Virtuoso.edmx

  18. Right click on the

    VirtuosoDataService

    project name of the

    Solution Explorer pane

    , then select the

    Add -> New Item

    menu options.

    Figure2.131.New Item

    New Item

  19. The

    Add New Item

    dialog will appear, choose the

    ADO.NET Data Service

    template, give it the name

    Virtuoso.svc

    and click

    Add

    to create the ADO.Net Data Service.

    Figure2.132.ADO.NET Data Service

    ADO.NET Data Service

  20. In the

    Virtuoso.svc.cs

    Data Service file created add the data source class name of

    VirtuosoDemoEntities

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

    DataService

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

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

    in the

    InitializeService

    method.

    // 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);
            }
        }
    }
    

    Figure2.133.Data Service

    Data Service

  21. To test the Data Service, simply hit

    Ctrl+F5

    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.

    Figure2.134.test the Data Service

    test the Data Service

  22. To access a specific entity instance like the

    Customers

    table

    ALFKI

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

    Figure2.135.Access a specific entity instance

    Access a specific entity instance

NOTES

  1. Important

    - To view

    Atom

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

    Feed Reading View

    is turned

    off

    . This can be done on the

    Content tab

    of

    Tools in Internet Options

    .

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

    Verbose

    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);
    
    }