Data Access with ADO.NET Data Services

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

Sign up for a Packt account to see the rest of this article

Now that you've read a few articles, you might want to consider signing up for a Packt account. It takes a matter of seconds, will give you access to all the articles on PacktPub.com, and once you've signed up you'll be returned here to carry on reading your article.

Furthermore, you'll gain access to nine free ebooks, and be offered a free trial of PacktLib, Packt's online library. Simply enter your details here, or log in to your existing account.

Log in

...or register

Post new comment

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
Sort A-Z