Accessing an ADO.NET Data Service with ASP.NET AJAX Part I

In this post, I describe how to get started with ADO.NET Data Services and ASP.NET AJAX.

If you have Visual Studio 2008 SP1, you have everything you need to create an ADO.NET Data Service.  I decided to make a website to show the speakers and sessions at Mix 09.  I created a standard ASP.NET web application called MixDataService.  I added a SQL Server 2005 database called Mix.mdf, and added three tables: Presenters, Sessions, and Links.

The Presenters table had eight fields:

  • ID
  • FirstName
  • LastName
  • Company
  • JobTitle
  • Email
  • Website
  • Description

The Sessions table had three fields:

  • ID
  • Name
  • Description

The Links table had three fields:

  • ID
  • PresenterID
  • SessionID

The database therefore allowed for a many-to-many relationship between presenters and sessions.  To create an ADO.NET Data Service, one must first create an ADO.NET Entity Data Model.  I selected Add New Item, and chose ADO.NET Entity Data Model, calling it Mix.  This starts the Entity Data Model Wizard.  In the first dialogue box, I chose Generate from database, rather than Empty model.  In the second dialogue, I selected the Mix database I had added earlier, and saved the entity connection settings in web.config as MixEntities.  In the third dialogue, I chose to include all my database tables in the model, and used the model namespace MixModel.  The program then generated the following model for me:

Model

The Entity Data Model having been generated, I could now create the Data Service.  I selected Add New Item, then ADO.NET Data Service.  I again used the name Mix.  I was then presented with the following code:

GeneratedCode

The TODO comments in the code give instructions on how to proceed.  Two changes need to be made.  On line 10, the comment needs to be replaced with the name of the entities generated earlier.  In my case, MixEntities.  By default, all access to data is forbidden.  To allow access, the access rules need to be changed, which can be done by uncommenting line 17.  For simplicity, I replaced MyEntityset in the above code with *, and AllRead with All.  This allows the client to make any changes to any data in the database.  This would never be appropriate for a public website, but it makes it easy to get started.  I hope to give more information about access rules in a future post.  Having made these changes, my code was as follows:

EditedCode

The service (in my case, Mix.svc) can now be viewed in a browser, and gives the following output:

ServiceOutput

For more information about ADO.NET Data Services, see, in addition to the videos mentioned on my previous post, the documentation on msdn.

Advertisements

One Response to “Accessing an ADO.NET Data Service with ASP.NET AJAX Part I”

  1. Summaries 15.04.2009 « Bogdan Brinzarea’s blog Says:

    […] Accessing an ADO.NET Data Service with ASP.NET AJAX (Part 1) […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: