<docbook><section><title>ODataServiceExample</title><para> </para>
<title>Creating an OData Publishing Endpoint using Virtuoso&#39;s ADO.NET Data Provider</title>Creating an OData Publishing Endpoint using Virtuoso&#39;s ADO.NET Data Provider
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> Introduction</bridgehead>
<bridgehead class="http://www.w3.org/1999/xhtml:h3"> What is OData?</bridgehead>
 <ulink url="http://www.odata.org/">OData</ulink> is an HTTP-based data protocol, with associated data representation formats (Atom and JSON).
 The underlying data model takes the form of an Entity-Attribute-Value graph, and originates from the Entity Frameworks aspect of ADO.NET.
 In this model, individual entities, entity attributes, and (optionally) attribute values are identified by Uniform Resource Identifiers (URIs).
 In short, OData decouples wire protocol data access and data representation formats from the ADO.NET platform.<para>The simplest service consists of a single feed hosting a basic Entity-Attribute-Value model tuple represented in Atom or JSON.
 Typically a service will provide a number of feeds, listed in the top-level service document.
 The model is published at the top level as a service-specific metadata document.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h3"> Why is OData Important?</bridgehead>
<para>The Open Data Protocol (OData) is a protocol for sharing data.
 Producers expose data as an OData service at an HTTP location (i.e., a URL) which is then available to OData consumers.
 As the OData protocol is based on the <ulink url="http://tools.ietf.org/html/rfc5023">AtomPub</ulink> standard, the simplest consumer can take the form of any HTTP user agent (e.g., a Web Browser), which is then able to browse entity-attribute-value model data stored within the Atom feeds.
 Likewise, Rich Internet Application clients, launched from within browsers or as stand-alone applications, such as the <ulink url="http://silverlight.net/content/samples/odataexplorer/">Silverlight OData Explorer</ulink>, may be used to browse this data.
 Either way, data from a range of disparate sources can be exposed as HTTP-accessible structured hypermedia resources (delivered by the Atom Feed).</para>
<para>By cleanly separating client and server roles, OData can serve as an HTTP analog of the platform-specific <ulink url="http://en.wikipedia.org/wiki/Open_Database_Connectivity">ODBC</ulink> (version 2.0, as of this writing).
 As with ODBC, data consuming applications can be developed independently from data sources, and a single application can consume data from a range of disparate sources.
 The OData service endpoint can be specified at runtime just as an ODBC Data Source Name (DSN) would be.</para>
<para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h3"> How are Virtuoso and OData Connected?</bridgehead>
<para>Virtuoso is a hybrid data server with native data management and integration capabilities covering RDF via SPARQL, ODBC or JDBC accessible Relational Data Sources; XML via XQuery/XPath; Web Services; and other HTTP-accessible Hypermedia and non-Hypermedia resources.
 Virtuoso includes a high-performance native ADO.NET data provider that provides access to its Relational-, RDF Graph-, and Document-model based storage engines, and thereby also makes data from these engines accessible to OData-based data consumers.</para>
<para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> How Do I use Virtuoso to Publish OData ?</bridgehead>
<para>There are two ways to produce OData from Virtuoso: one approach uses Virtuoso&#39;s built-in support of the lightweight and platform-agnostic OData protocol; the other uses Virtuoso&#39;s ADO.NET provider.
 This guide covers use of the ADO.NET provider.</para>
<para>To create an OData service endpoint using Virtuoso and its ADO.NET provider, you will need the following in place:</para>
<itemizedlist mark="bullet" spacing="compact"><listitem>Microsoft Visual Studio 2010 </listitem>
<listitem>The ADO.NET Entity Framework 4.0 runtime and associated tools (included in Visual Studio 2010, but not installed by default)</listitem>
</itemizedlist><para>At the start of your Visual Studio project creation, you must establish a connection with the database(s) hosting the data to be exposed by the service.
 Once a connection has been established, you can generate a data model which lays the foundation for publishing an OData service.</para>
<para>The following steps show how to expose the Northwind Demo database in Virtuoso as an OData Service using the Virtuoso ADO.NET Provider: </para>
<orderedlist spacing="compact"><listitem>Launch the <emphasis>Visual Studio 2010 IDE</emphasis>.
</listitem>
<listitem>Choose <emphasis>New Project</emphasis> from the <emphasis>File</emphasis> menu to create a <emphasis>Web Application</emphasis> project.
</listitem>
<listitem>When the <emphasis>New Project</emphasis> window appears, choose either <emphasis>Visual Basic</emphasis> or <emphasis>Visual C#</emphasis> as the programming language.
</listitem>
<listitem>Within the language category, click on <emphasis>Web</emphasis>, and select <emphasis>ASP.NET Web Application</emphasis> from the right-hand panel.
<ulink url="ODataServiceExample/NewProject.png"><figure><graphic fileref="ODataServiceExample/NewProject.png" /></figure></ulink> </listitem>
<listitem>Choose a name for the project, for example ODataWebApplication, and click OK.
</listitem>
<listitem>This will create a new project called OdataWebApplication.
<ulink url="ODataServiceExample/ODataWebAppProject.png"><figure><graphic fileref="ODataServiceExample/ODataWebAppProject.png" /></figure></ulink> </listitem>
<listitem>To set up the database connection and generate the data model, right click on the OdataWebApplication project name in the <emphasis>Solution Explorer</emphasis> pane; then select the <emphasis>Add -&gt; New Item</emphasis> menu item.
</listitem>
<listitem>The <emphasis>Add New Item</emphasis> dialog will appear.
 Choose <emphasis>Data</emphasis> from the <emphasis>Installed Templates</emphasis>; then select the <emphasis>ADO.NET Entity Data Model</emphasis> template from the right-hand pane.
 Name the model <emphasis>DemoModel.edmx</emphasis>, and click <emphasis>Add</emphasis> to start the creation of the ADO.NET Entity Data Model.
<ulink url="ODataServiceExample/AddModel.png"><figure><graphic fileref="ODataServiceExample/AddModel.png" /></figure></ulink> </listitem>
<listitem>In the <emphasis>Entity Data Model Wizard</emphasis> dialog&#39;s <emphasis>Choose Model Contents</emphasis> pane, select the <emphasis>Generate from Database</emphasis> model type and click <emphasis>Next</emphasis>.
<ulink url="ODataServiceExample/EdmWizard.png"><figure><graphic fileref="ODataServiceExample/EdmWizard.png" /></figure></ulink> </listitem>
<listitem>In the <emphasis>Entity Data Model Wizard</emphasis> dialog&#39;s <emphasis>Choose your Data Connection</emphasis> pane, select the <emphasis>New Connection</emphasis> button.
<ulink url="ODataServiceExample/EdmWizard2.png"><figure><graphic fileref="ODataServiceExample/EdmWizard2.png" /></figure></ulink> </listitem>
<listitem>In the <emphasis>Choose Data Source</emphasis> dialog, select the <emphasis><ulink url="OpenLink">OpenLink</ulink> Virtuoso Data Source</emphasis> from the list displayed and click <emphasis>Continue</emphasis>.
<ulink url="ODataServiceExample/ChangeDataSource.png"><figure><graphic fileref="ODataServiceExample/ChangeDataSource.png" /></figure></ulink> </listitem>
<listitem>In the <emphasis>Add Connection</emphasis> dialog, specify the <emphasis>hostname</emphasis>, <emphasis>port number</emphasis>, <emphasis>username</emphasis>, and <emphasis>password</emphasis> for the target Virtuoso Server and check the <emphasis>Save Password</emphasis> box.
<ulink url="ODataServiceExample/ConnProps2.png"><figure><graphic fileref="ODataServiceExample/ConnProps2.png" /></figure></ulink> </listitem>
<listitem>Select the <emphasis>Select Database From List</emphasis> radio button and choose <emphasis>Demo</emphasis> from the drop down list, assuming the <emphasis>Virtuoso Demo Database</emphasis> is installed.
<ulink url="ODataServiceExample/ChooseDatabase.png"><figure><graphic fileref="ODataServiceExample/ChooseDatabase.png" /></figure></ulink> </listitem>
<listitem>Click the <emphasis>Test Connection</emphasis> button to verify the connection is successful and then click <emphasis>OK</emphasis> to add the connection.
</listitem>
<listitem>Set the entity connect string name to <emphasis>VirtuosoDemoEntities</emphasis> (remember the name you set here, as it is required in step 20 below) and click <emphasis>Next</emphasis>.
<ulink url="ODataServiceExample/EdmWizard3.png"><figure><graphic fileref="ODataServiceExample/EdmWizard3.png" /></figure></ulink> </listitem>
<listitem>In the <emphasis>Choose your Database Objects</emphasis> pane, tick the <emphasis>Tables</emphasis> check-box to select all tables in the Demo database for addition to the Entity Data Model, set the Model Namespace to <emphasis>VirtuosoDemoModel</emphasis>, and click <emphasis>Finish</emphasis>.
<ulink url="ODataServiceExample/EdmWizard4.png"><figure><graphic fileref="ODataServiceExample/EdmWizard4.png" /></figure></ulink> </listitem>
<listitem>The <emphasis>VirtuosoDemoModel.edmx</emphasis> EDM will be created, with the tables and relationships displayed in the Visual Studio IDE.
<ulink url="ODataServiceExample/DataModel.png"><figure><graphic fileref="ODataServiceExample/DataModel.png" /></figure></ulink> </listitem>
<listitem>Now to create the service.
 Right click on the <emphasis>ODataWebApplication</emphasis> project name of the Solution Explorer pane, then select the <emphasis>Add -&gt; New Item</emphasis> menu item.
</listitem>
<listitem>The <emphasis>Add New Item</emphasis> dialog will appear.
 Select <emphasis>Web</emphasis> from the installed templates, and choose the <emphasis>WCF Data Service</emphasis> template in the right-hand pane.
 Name the service <emphasis>VirtuosoOData.svc</emphasis>, and click <emphasis>Add</emphasis> to create the OData Service.
<ulink url="ODataServiceExample/AddNewService2.png"><figure><graphic fileref="ODataServiceExample/AddNewService2.png" /></figure></ulink> </listitem>
<listitem>In the <emphasis>VirtuosoOData.svc.cs</emphasis> Data Service file created by the steps above, add the data source class name of <emphasis>VirtuosoDemoEntities</emphasis> (note: this is the name set in step 15, above) as the DataService name, and enable access to the Data Service by adding the entry <emphasis>config.SetEntitySetAccessRule(&quot;*&quot;, EntitySetRights.All);</emphasis> in the InitializeService method.
<programlisting>public static void InitializeService(DataServiceConfiguration config)
  {
     // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
     // Examples:
     // config.SetEntitySetAccessRule(&quot;MyEntityset&quot;, EntitySetRights.AllRead);
     // config.SetServiceOperationAccessRule(&quot;MyServiceOperation&quot;, ServiceOperationRights.All);
     config.SetEntitySetAccessRule(&quot;*&quot;, EntitySetRights.All);
     config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
  }
</programlisting></listitem>
<listitem>To test the OData Service, simply hit Ctrl+F5 within Visual Studio, which will start the development web server, run the OData Services server therein, and load a Web browser page displaying the list of available tables/entities of the Demo database.
<ulink url="ODataServiceExample/TestService.png"><figure><graphic fileref="ODataServiceExample/TestService.png" /></figure></ulink></listitem>
</orderedlist><bridgehead class="http://www.w3.org/1999/xhtml:h2"> How do I Consume the Published OData?</bridgehead>
<para>The simplest way to consume the service is to use a web browser to browse the Atom feed.
 This is what is done when testing the service in Visual Studio.
 As well as the top-level list of feeds provided by the service, it is straightforward to access a specific entity instance.
 For example, to access the ALFKI record of the Customers table, simply append <emphasis>/Customers(&#39;ALFKI&#39;)</emphasis> to the service root URI.
<ulink url="ODataServiceExample/SingleRecord.png"><figure><graphic fileref="ODataServiceExample/SingleRecord.png" /></figure></ulink> Similarly, the metatdata document can examined by appending /$metadata to the service instance URI.
 The metadata document is useful for finding information about the entities provided on the service.</para>
<para>Alternatively, the service can be consumed by a more sophisticated application like the <ulink url="http://silverlight.net/content/samples/odataexplorer/">Silverlight OData Explorer</ulink>.</para>
<orderedlist spacing="compact"><listitem>While the OData service is running, open the Silverlight OData Explorer as a stand-alone application on the local machine.
</listitem>
<listitem>Choose a workspace name, and give your data service root URI.
<ulink url="ODataServiceExample/VirtuosoServiceUri.png"><figure><graphic fileref="ODataServiceExample/VirtuosoServiceUri.png" /></figure></ulink> </listitem>
<listitem>The entity types available on your service are listed in the <emphasis>Collections</emphasis> box, and by selecting one, you can see a list of all entities of the chosen type.
<ulink url="ODataServiceExample/ViewCollection.png"><figure><graphic fileref="ODataServiceExample/ViewCollection.png" /></figure></ulink></listitem>
</orderedlist><para>The Silverlight OData Explorer also allows you to update data via the service.
 For example:</para>
<orderedlist spacing="compact"><listitem>List the Customers entities.
<ulink url="ODataServiceExample/OriginalData.png"><figure><graphic fileref="ODataServiceExample/OriginalData.png" /></figure></ulink> </listitem>
<listitem>Click the <emphasis>Edit</emphasis> button at the left of the record you wish to update.
 This opens an edit dialog where you can make changes to the attributes of the entity.
<ulink url="ODataServiceExample/EntityDetails.png"><figure><graphic fileref="ODataServiceExample/EntityDetails.png" /></figure></ulink> </listitem>
<listitem>By clicking <emphasis>SaveChanges</emphasis>, you propagate your edits back to the underlying data store.
 If you close the edit box and reload the list of entities, you will see that the record has been updated.
<ulink url="ODataServiceExample/UpdatedData.png"><figure><graphic fileref="ODataServiceExample/UpdatedData.png" /></figure></ulink></listitem>
</orderedlist><bridgehead class="http://www.w3.org/1999/xhtml:h3"> Notes</bridgehead>
<orderedlist spacing="compact"><listitem><emphasis>Important</emphasis> - To view Atom data (the default format returned by an ADO.NET Data Service) in Internet Explorer, you must first ensure that <emphasis>Feed Reading View<emphasis /> is turned off.
 This can be done on the <emphasis>Content</emphasis> tab of <emphasis>Tools</emphasis> in <emphasis>Internet Options</emphasis>.
</emphasis></listitem>
<listitem>If a Data Services entity instance URI page fails to load, turn <emphasis>Verbose error</emphasis>s on by adding <emphasis>config.UseVerboseErrors = true;</emphasis> to the virtuoso.svc.cs InitializeService method to obtain more detailed information from the server as to why the page failed to load: <programlisting>      public static void InitializeService(IDataServiceConfiguration config)
      {
      config.UseVerboseErrors = true;
      config.SetEntitySetAccessRule(&quot;*&quot;, EntitySetRights.All);
      }
</programlisting> </listitem>
</orderedlist><bridgehead class="http://www.w3.org/1999/xhtml:h2"> Related</bridgehead>
<itemizedlist mark="bullet" spacing="compact"><listitem><ulink url="VirtRDFODataExample">Acessing RDF Triples as OData Service</ulink> </listitem>
<listitem><ulink url="VirtAdoNet35Provider">Virtuoso ADO.NET 3.5 Provider</ulink> </listitem>
<listitem><ulink url="http://www.odata.org/">Open Data Protocol</ulink> </listitem>
<listitem><ulink url="http://msdn.microsoft.com/en-us/library/dd831853.aspx">Visual Studio 2010</ulink></listitem>
</itemizedlist></section></docbook>