Data Access with ADO.NET Data Services

Exclusive offer: get 50% off this eBook here
Entity Framework Tutorial

Entity Framework Tutorial — Save 50%

Learn to build a better data access layer with the ADO.NET Entity Framework and ADO.NET Data Services with this book and eBook

$23.99    $12.00
by Jayaram Krishnaswamy | March 2009 | .NET Microsoft

ADO.NET Data Services leverages the ADO.NET Entity Framework to build data services for relational data sources. These include support for MySQL, DB2, and Oracle in addition to MS SQL Server. It can also be used with non-relational data using an add-on provider model. With ADO.NET Data Services, the developers can build dynamic internet applications (AJAX, Silverlight, etc) that works across intranets as well as the internet. With this in place data access by applications is made possible using URI Syntax and using HTTP verbs operating on the data.

In this article by Dr. Jayaram Krishnaswamy, you will learn how to create a ADO.NET Data Service from scratch. You will also learn how you may access data using the URI constructs. The backend data for this tutorial comes from a copy of Northwind database named TestNorthwind on a named instance of SQL Server 2008. In order to work through this tutorial you should have Visual Studio 2008 SP1 installed. You also need to have .NET Framework 3.5 installed.

In essence, ADO.NET Data Services serves up data as a web service over HTTP using the objects in the Entity Model of the data and conforms to the principles of REST (Representational State Transfer) wherein everything is a kind of resource with a name and they can be manipulated using the well known HTTP Verbs; Get, Put, Post and Delete. The data can be returned in a number of formats including RSS and ATOM. According to W3C, "Atom" is an XML-based document format that describes lists of related information known as "feeds". Feeds are composed of a number of items, known as "entries", each with an extensible set of attached "metadata".

Create an ASP.NET Web Application

Create an ASP.NET Web application as shown below and make sure the target platform is 3.5.

Entity Framework Tutorial

Create the Entity Model

Right click the Application in Solution Explorer and click on Add New Item. In the Add New Item window click on ADO.NET Entity Data Model and change the default name Model1.edmx to one of your choice. Here it is MyFirst.edmx. Then click on the Add button.

Entity Framework Tutorial

This brings up the Entity Data Model Wizard as shown. Read the notes on this page.

Entity Framework Tutorial

Now click on the Generate from Database and click Next. This opens the 'Choose Your Data Connection' page of the wizard as shown with some default connection as shown.

Entity Framework Tutorial

Change it over to the TestNorthwind database on SQL Server 2008 Enterprise Edition (RTM). TestNorthwind is a copy of the Northwind database and click Next. Of course you can create a new connection as well. The connection is saved to the Web.Config file.

Entity Framework Tutorial

Click on the Next button. The wizard changes page so that you can 'Choose your Database Objects' This may take some time as the program accesses the database and the following page is displayed.

Entity Framework Tutorial

The model uses the relational data in the database. Place a check mark for the Tables to include all the tables in the database. The TestNorthwindModel namespace gets created. Click on the Finish button. This brings up the ModelBrowser browsing the MyFirst.edmx file as shown.

Entity Framework Tutorial

The MyFirst.edmx file contents are shown in the next figure. By right clicking inside the MyFirst.edmx you can choose the Zoom level for the display.

Entity Framework Tutorial

The connected items shown are the tables with relationships in the database. Those standing alone are just tables that are not a part of the original database and can be removed by right clicking and deleting them.

Entity Framework Tutorial

The tables retained finally for the model are as shown. Make use of the Zoom controls to adjust to your comfort level.

Entity Framework Tutorial

As seen in the above each table has both Scalar Properties as well as Navigation Properties. The Navigation properties shows the relationship with other tables. In the OrderDetails table shown, the connections extend to the Orders table and the Products table.

Entity Framework Tutorial

On the other hand the Orders table is connected to, Customers, Employees, Order_Details and Shippers.

In the Solution Explorer the MyFirst.edmx file consists of the TestNorthwindModel and the TestNorthwindModel.store folders. The model folder consist of the Entities and the Assoications(relationships between entities) and the EntityContainer as shown.

Entity Framework Tutorial

Entity Framework Tutorial Learn to build a better data access layer with the ADO.NET Entity Framework and ADO.NET Data Services with this book and eBook
Published: October 2008
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

Creating a Data Service

Right click the application in the Solution Explorer and click on Add New Item. In the Add New Item window that shows up click on ADO.NET Data Service as shown and provide a name for the service; MyADODS in the present case.

Entity Framework Tutorial

This adds a MyADODS.svc file to the Solution Explorer which is a template for the service. MyADODS.svc is an XML file that you can browse to and since it is not fully configured, what you see will be what is shown in the next figure. Note the reference to ATOM, an XML-based Web content and metadata syndication format.

Entity Framework Tutorial

The service template file is referenced by the System.Data.Services and also the System. ServiceModel.Web as you see in this template code. By default the visibility (access) for this is not set.

Initialize the Data Service

Imports System.Data.Services
Imports System.Linq
Imports System.ServiceModel.Web

Public Class MyADODS
' TODO: replace [[class name]] with your data class name
Inherits DataService(Of [[class name]])

' This method is called only once to initialize service-wide policies.
Public Shared Sub InitializeService(ByVal _
config As IDataServiceConfiguration)
' TODO: set rules to indicate which entity sets and service
'operations are visible, updatable, etc.
' Examples:
' config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead)
' config.SetServiceOperationAccessRule("MyServiceOperation",
'ServiceOperationRights.All)
End Sub

End Class

If you follow the TODO list, you first need to replace the class name with the data class name. You can pick up the right class to associate using the intellisense drop-down as shown.

Entity Framework Tutorial

The next thing to do is to initialize the service and the template provides examples of how this can be done. Presently the template does not provide access to data in the data services. The next step of the TODO therefore is for providing the visibility to the data. The way to code this access is shown in the next picture of the intellisense drop-down. The wild card is used for all the objects in the model and everyone has access to the entity sets.

Entity Framework Tutorial

The completed code is shown in the next paragraph.

Imports System.Data.Services
Imports System.Linq
Imports System.ServiceModel.Web

Public Class MyADODS
' TODO: replace [[class name]] with your data class name
Inherits DataService(Of TestNorthwindEntities)

' This method is called only once to initialize service-wide policies.
Public Shared Sub InitializeService(ByVal _
config As IDataServiceConfiguration)

config.SetEntitySetAccessRule("*", EntitySetRights.All)
End Sub

End Class

Since the access is now granted, the MyADODS.SVC file gets displayed as shown when you browse the file. You can see the content that you can now access and shows everything contained in the model.

Entity Framework Tutorial

Examine URI access to data

You can access any particular collection by the URI as shown in the next for Categories entity. This is what you will see in IE by default (View set to display feeds). This is the default setting in IE for the ATOM feeds and can be turned off using the internet option.

Entity Framework Tutorial

If you were to view the same file in another browser, say Google Chrome, you may actually see some of the contents as shown here.

Entity Framework Tutorial

The next two figures shows the steps required to turn off the feeds in Internet Explorer. The details are available at the following link:http://hodentekhelp.blogspot.com/2008/12/how-do-i-turn-off-atom-rss-feeds-in-ie.html

Entity Framework Tutorial

Entity Framework Tutorial

When you turn off the feed you can get to see the XML content of what you see in the display for the URI in the address. This shows all the 8 categories in the Category entity. For each category entry you will get to see all the details such as name, data type etc.

Entity Framework Tutorial

Going further you can access any of them as shown in the next figure for Category(1). Note the "id" property in the next figure to make this access [http://localhost:1907/MyADODS.svc/Categories(1)]. This is as simple as this.

Entity Framework Tutorial

You can drill down further to see the 'Name' in the category(1) by the uri access, http://localhost:1907/MyADODS.svc/Categories(1)/CategoryName which shows the following:

Entity Framework Tutorial

What about the binary file in this entity? You can get access to that as well using the same URI access Syntax as follows:

Entity Framework Tutorial

The value returned is a Base64 binary encoded string which is the same as what one would get by the following query run against the TestNorthwind database as shown here.

Entity Framework Tutorial

URI access to related data

Because of the existing relationships in the tables which are reflected in the entities, you can also access the data in the related tables. The Links in the SVC file can be used to access information in the table(s) linked to the Categories table as shown in this link tag.

<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories" 
type="application/atom+xml;
type=entry"
title="Categories"
href="Products(1)/Categories" />

For example, from among the several products associated with Category(1), you can access Products(1) in Category(1) as in the following URI.

http://localhost:1907/MyADODS.svc/Categories(1)/Products(1)

This would which display the info for Products(1) associated with the Category(1) which is a 'beverage'.

Entity Framework Tutorial

Summary

The tutorial describes the process of setting up a web application to work with ADO.NET Data Services by first creating an Entity Model. The ADO.NET Data Service (a.k.a Astoria) uses the entity model as backend. Using the URI to access data from the entities and related objects with HTTP is also described for the Northwind relational database.

If you have read this article you may be interested to view :

Entity Framework Tutorial Learn to build a better data access layer with the ADO.NET Entity Framework and ADO.NET Data Services with this book and eBook
Published: October 2008
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


Jayaram Krishnaswamy

Jayaram Krishnaswamy studied at the Indian Institute of Science in Bangalore India and Madras University in India and taught at the Indian Institute of Technology in Madras. He went to Japan on a Japanese Ministry of Education Research scholarship to complete his PhD in Electrical Engineering from Nagoya University. He was a Post-Doctoral Fellow at Sydney University in Australia; a Government of India Senior Scientific Officer at the Indian Institute of Science in Bangalore and Indian Institute of Technology at Kanpur; a Visiting Scientist at the Eindhoven Institute of Technology in the Netherlands; a visiting Professor of Physics at the Federal University in Brazil; an Associate Research Scientist at a government laboratory in São Jose dos Campos in São Paulo, Brazil; a visiting scientist at the National Research Council in Ottawa, Canada before coming to USA in 1985. He has also taught and worked at the Colorado State University in Fort Collins and North Carolina State University in Raleigh, North Carolina. He worked with Northrop Grumman Corporation on a number of projects related to high energy electron accelerators and Free Electron Lasers. These projects were undertaken at the Brookhaven National Laboratory in Long Island and in the Physics Department at Princeton University. He has over 80 publications in refereed and non-refereed publications and 8 issued patents. He is fluent in Japanese and Portuguese and lives in Honolulu, Hawaii, USA.

He has been working in IT-related fields since 1997. He was once a Microsoft Certified Trainer in Networking and a Siebel Certified developer. He has worked with several IT related companies, such as the Butler International in their Siebel practice, with several IBM sub-contractors and smaller companies. Presently he is active in writing technical articles in the IT field to many online sites such as http://CodeProject.com, http://APSFree.com, http://DevShed.com, http://DevArticles.com, http://OfficeUsers.org, http://ASPAlliance.com, Egghead Café, http://SSWUG.org, Packt Article Network, http://databasedev.co.uk, http://cimaware.com, and many others. Between 2006 and 2010 he wrote more than 400 articles mostly related to database and web related technologies covering Microsoft, Oracle, Sybase, ColdFusion, Sun, and other vendor products.

He has written four books all published by Packt related to Microsoft Database and Application Development: SQL Server Integration Services Using Visual Studio 2005, Learning SQL Server Reporting Services 2008, Microsoft SQL Azure; Enterprise Application Development, and Microsoft Visual Studio Lightswitch Business Application Development. He regularly writes for his four blogs on Blogger; http://Hodentek.blogspot.com, http://HodentekHelp.blogspot.com, http://HodentekMobile.blogspot.com, and http://HodentekMSSS.blogspot.com. He received the 2011 Microsoft Community Contributor award.

Contact Jayaram Krishnaswamy

Books From Packt

ASP.NET 3.5 Social Networking
ASP.NET 3.5 Social Networking

DotNetNuke Skinning Tutorial
DotNetNuke Skinning Tutorial

VSTO 3.0 for Office 2007 Programming
VSTO 3.0 for Office 2007 Programming

Implementing Microsoft Dynamics NAV 2009
Implementing Microsoft Dynamics NAV 2009

ASP.NET 3.5 Application Architecture and Design
ASP.NET 3.5 Application Architecture and Design

ASP.NET Data Presentation Controls Essentials
ASP.NET Data Presentation Controls Essentials

Software Testing with Visual Studio Team System 2008
Software Testing with Visual Studio Team System 2008

C# 2008 and 2005 Threaded Programming: Beginner's Guide
C# 2008 and 2005 Threaded Programming: Beginner's Guide

 

Your rating: None Average: 4.7 (3 votes)

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
D
8
p
5
T
c
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software