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:
The Sessions table had three fields:
The Links table had three fields:
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:
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:
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:
The service (in my case, Mix.svc) can now be viewed in a browser, and gives the following output:
For more information about ADO.NET Data Services, see, in addition to the videos mentioned on my previous post, the documentation on msdn.