RIA Services - using Entity Framework


In Silverlight Business Application the data can come from any kind of data source. The RIA service makes it easy to work with any of the data source in seamless way.

In this article I will walk through step by step to demonstrate how Entity Framework can be used to access SQL database. For this demonstration I will use SQL Express database as data source.

1. Create Silverlight Business Application

Create new Silverlight Business Application project. I gave the application name "RIASample", you may choose give name as per your requirement.

1.gif

When you click Ok button in New Project dialog box, two projects will be added into your application solution.

2.gif

2. Add database

Right click on "App_Data" folder in "RIASample.Web" project, and select Add > New Item. From the New Item dialog box select "SQL Server database", give file name "Student.mdb" and click on Add button.

3.gif

Once the database is added create two tables Student and Course (you can get table structure in attached code).

3. Add ADO.Net Entity Data Model

To add ADO.Net Entity Data Model, right click on "RIASample.Web" project and select Add > New Item. From New Item dialog box select "ADO.Net Entity Data Model" template, give file name "Student.edmx" and click on Add button. This will open "Entity Data Model Wizard".

4.gif
 
From the Entity Data Model Wizard, select "Generate from database" and click on Next button. Here you are specifying what your model should contain, as we have planned to use database, I selected Generate from database.

5.gif
 
As soon as you click on Next button the Wizard will show next section "Choose Your Data Connection". By default "Student.mdb" is selected, if you want to choose different database, you may click on "New Connection…" button to link different database. You can see the Entity connect string in this dialog box. By default Entity connection setting will be saving in Wb.config with the name "StudentEntity", you may change this to different name. 

Click on next Button.

6.gif
 
In next section select the database object you want to include in entity and click on Finish button. You can see the Student and Course tables are loaded in entity.

7.gif

4. Add Domain Service

To consume data in Silverlight client we have to write business logic using Domain Service. Domain Service is a special type of WCF service which provides easier solution to query, update and validate the data.

To Add Domain Service right click on "RIASample.Web" project, select Add > New Item. From New Item dialog box select "Domain Service Class" template, give domain service name "StudentDomainService.cs" and click on Add button.

8.gif

As soon as you click on Add button, it will open "Add New Domain Service Class" dialog box. This dialog box will show all entity frameworks added in your project, select the "StudentEntity" entity framework, this will show all the entities available in "StudentEntity", select them and click on Ok button.

9.gif

A "StudentDomainService.cs" file will be added into project with below generated code.

[EnableClientAccess()]
public class StudentDomainService : LinqToEntitiesDomainService<StudentEntities>
{
        public IQueryable<Student> GetStudents()
        {
            return this.ObjectContext.Students;
        }
}

The EnableClientAccess attribute in class marks Domain Service accessible over http.

5. Consume Domain Service in Silverlight client

Before you start coding for Silverlight client, build your solution. 

Add below line of code in "Home.xaml".

<my:DataGrid x:Name="StudentGrid" Height="100" Width="300" ItemsSource="{Binding}" />

Import RIASample.Web namespace in "Home.xaml.cs", once it is imported you will be able to use domain context directly.

using RIASample.Web;

Load domain context and assign item source for data grid. To do this create student domain context object and load student data in domain context, once the student data is loaded, you can set data grid item source.

var studentContext = new StudentDomainContext();
studentContext.Load(studentContext.GetStudentsQuery());
StudentGrid.ItemsSource = studentContext.Students;

6. Run Application

Now finally run your application. You can see the data grid with student's information.

10.gif 

In this article we have seen approach to use Entity Framework to consume data using RIA services in Silverlight client. 


Similar Articles