ADO.NET Data Model and ADO.NET Data Service in Silverlight 3


Introduction

In this article we would see how we can use ADO.NET Entity Data Model as the ORM in Silverlight 3 Application, and ADO.NET Data Service to fetch the data.

Creating Silverlight Project

Fire up Visual Studio 2008 and create a new Silverlight 3 Project. Name it as ADONET_Service_SL3_Sample.

ADO.NETSilverlight1.gif

Here is the assumption that you must understand is that I have a DataBase "EmployeeDB" which has a table "Employee_Basic_Info".

Now add a ADO.NET Entity Data Model (EmployeeModel.edmx) to the web project as displayed in below figure.

ADO.NETSilverlight2.gif

The following screens are for the reference of connecting to the SQL Server, and selecting table from the Database.

ADO.NETSilverlight3.gif

ADO.NETSilverlight4.gif

ADO.NETSilverlight5.gif

After you are done with the connection and selecting tables from the Database you would have the Entity Design as displyed in below figure. I have changed the original names to a good readable name set.

ADO.NETSilverlight6.gif

You can find the Mapping details in the below figure.

ADO.NETSilverlight7.gif

The following figure displays the heirarchical structure of the Model.

ADO.NETSilverlight8.gif

Now we will add a ADO.NET Data Service to the web project, name it as EmployeeWebDataService.svc

ADO.NETSilverlight9.gif

After adding the service the VS explorer will open up the cs file for you. There you need to add the EmployeeDBEntities for DataService and uncomment the line(s) for config settings.

The following setting are for Read and Write.

ADO.NETSilverlight10.gif

After you are done with the above settings in the svc.cs file. You can see the service in browser as below figure.

ADO.NETSilverlight11.gif

Now we need to add the reference to the Silverlight Project, as follows:

ADO.NETSilverlight12.gif

Now we would make a simple UI which will hold a DataGrid and few Buttons and TextBoxes for the related queries.

ADO.NETSilverlight13.gif

Now for ShowAll Button Click add the following code, before that we need to have a context of the Entity.

ADO.NETSilverlight14.gif

Add the following code for Show All Button Click event.

ADO.NETSilverlight15.gif

Now add the following code for Search Button Click.

ADO.NETSilverlight16.gif

That's it we are done. Now run the application.

ADO.NETSilverlight17.gif

Hope this article helps.

 

Up Next
    Ebook Download
    View all
    Learn
    View all