ADO.NET Data Services : How to create an ADO.NET Data Services with Visual Studio 2010 beta2

Introduction:

This post is the first of a series of articles based on Visual Studio 2010. In this one I will introduce you to publishing your data through ADO.NET Data Services. Here are the main interest for me into using Data Services:

  • Exposing your data as a Data Service allows clients to consume it easily from using the technology you want (WPF, Silverlight, ASP.NET, J2EE, …);
  • Addressing Scheme for Data with URIs;
  • Data Transport Format ubiquitous (AtomPub or JSON);
  • Data Storage independence;
  • Usage of REST semantic;

Pre-requisites:

Visual Studio 2010 beta2 + SQL Server 2005 Express or 2008 Express https://msdn.microsoft.com/en-us/vstudio/dd582936.aspx

AdventureWorks database https://www.codeplex.com/MSFTDBProdSamples/Release/ProjectReleases.aspx?ReleaseId=18407

Step 1 : Create your web application project

After installing all software pre-requisites, let’s begin.

Open Visual Studio 2010 beta 2 and create an empty ASP.NET Web application named MyDataServicesApplication, for example. This web project will contains the edmx (i.e. the Entity Data Model) file and the ADO.NET Data Services class.

image

Step 2 : Create an Entity Model of the AdventureWorks database

First of all, a edmx file has to be created.

Create a new Item, select ADO.NET Entity Data Model item template, name it AdventureWorksModel

image

Press the Add button and follow the configuration wizard step by step

image

Select Generate from database to generate the edmx file from the existing database structure.

image

Select the AdventureWorks database

image

Specify the name of the connection string used, the specified name is stored in the web.config file into the connectionString tags.

image

Click the checkbox next to the Tables element, in order to include all existing tables into your data model.

Check the Pluralize or singularize generated object names check box and specify AdventureWorksModel as the namespace. This will pluralize all collection property names, this is a new feature of Visual Studio 2010.

Step 3 : Create the ADO.NET Data Service

Create a new item, Select the ADO.NET Data Service template and name it AdventuWorksService.svc

Open AdventureWorksService.svc.cs and update the content to be like this:

Code Snippet

  1.  
  2. namespace MyDataServicesApplication
  3. {
  4.     public class AdventureWorksService : DataService<AdventureWorksEntities>
  5.     {
  6.         // This method is called only once to initialize service-wide policies.
  7.         public static void InitializeService(DataServiceConfiguration config)
  8.         {
  9.             // Grant full access to all exsisting entities
  10.             config.SetEntitySetAccessRule("*", EntitySetRights.All);
  11.             // Grant full access for all operations
  12.             config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
  13.             config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
  14.         }
  15.     }
  16. }

Step 4 : Test

Press F5 to check the result, it should list all the available entities :

image

You can now request your database through REST, for example select all the products using this query (which is now just an URI!): https://localhost:34570/AdventureWorksService.svc/Products

image

The result is represented by default using the ATOM Publishing Protocol. Internet Explorer automatically applies a style on ATOM format to make it more readable. Here this is not the case :-). I suggest that you right click on the content of the page and select View Source, the result will be more user friendly.

image

Here is a link of the rest Services sementics, a usefull link : https://msdn.microsoft.com/en-us/library/cc668767.aspx

 

Conclusion

Here is a step by step guide for creating an ADO.NET Data Services with Visual Studio 2010 beta2.