Data Access with ADO.NET Data Services

Entity Framework Tutorial


October 2008

$12.00

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

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

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 :

Books to Consider

comments powered by Disqus