Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Silverlight / Silverlight4

Step By Step Guide to WCF RIA Enabled SL4 Application with Entity Framework

0.00/5 (No votes)
12 Apr 2011CPOL2 min read 7.3K  
We will create a Silverlight 4 based application with WCF RIA services and Entity Framework

In my last post, we discussed about the concepts of WCF Ria services , this post is a continuation of my earlier post.Here we will create a Silverlight 4 based application with WCF RIA services and Entity Framework.Before going through the article make sure you have downloaded the latest SDKs from Microsoft as mention in my last post.

Well we will develop a simple database centric user management system where the administrator is going to view and edit user detail.For this app we will use remote sqlserver with following tables.

Entity Framework, Silverlight 4.0, WCF RIA Service

Create the Project

  • Open VS 2010 , Select Microsoft installed Silverlight Business Template and click ok.Once Project Created Compile the application.You will find a structured application with pre defined WCF RIA support .It will also have predefined user registration and login module

Entity Framework, Silverlight 4.0, WCF RIA Service

Entity Framework, Silverlight 4.0, WCF RIA Service

Generate Data Model through EF at Server Project

  • Add ADO.NET Entity model to server side Project.
  • Here I am using remote SQL Server from shared hosting, so select connection and proceed.

Entity Framework, Silverlight 4.0, WCF RIA Service

Entity Framework, Silverlight 4.0, WCF RIA Service

  • More detailed description of adding EF can be found from this blog post.

Add Domain Service to Server Project

  • Add a new Item to server side project name “UM_Server_DomainService.cs”.Before adding, make sure that you have compiled the project.

Entity Framework, Silverlight 4.0, WCF RIA Service

Entity Framework, Silverlight 4.0, WCF RIA Service

  • This domain service class will expose the data entities and operations of server side project to the client side project.

Entity Framework, Silverlight 4.0, WCF RIA Service

This will add up 2 classes as shown in the figure.

Entity Framework, Silverlight 4.0, WCF RIA Service

  • Compile the project, next we will move on the client side project for data binding.

Load Controls, using the Data Context at Client Side Project (Data Binding Through Code)

  • If you check the hidden folder “Generated_Code“ you will find auto generated DataContext code for the client.

Entity Framework, Silverlight 4.0, WCF RIA Service

  • From DomainClient, we can communicate to the domain service at server. From client side, we can perform 3 fundamental kinds of operations such as Query, Invoke and Submit. (Details regarding it are out of scope of this post, still you can refer here).
  • So to bind the data, go to Home Page and add a Drag and Drop a combo box.
  • Next steps will be:

Creating a Query Based Operation

Assigning Item data source to the control

C#
private void Page_Loaded(object sender, System.Windows.RoutedEventArgs e) 
{ 
UserManagementSystem.Web.UM_Server_DomainContext context = 
new UserManagementSystem.Web.UM_Server_DomainContext(); 

LoadOperation<IT_USER> usersQueryOperation = context.Load(context.GetIT_USERQuery()); 
cmbUsers.ItemsSource = usersQueryOperation.Entities; 
cmbUsers.DisplayMemberPath = "UserName"; 
}

So the code as above will fetch the data and load the users.

Entity Framework, Silverlight 4.0, WCF RIA Service

Alternatively, we can also use the Drag and Drop feature of VS 2010 to create binding. Simply go to Home page and open the Data Source Navigation pane.

Entity Framework, Silverlight 4.0, WCF RIA Service

Entity Framework, Silverlight 4.0, WCF RIA Service

That’s it, is not it cool? For my goodness, the whole lot of work suddenly looks so simple, hats off to Microsoft :).

Your comments are welcome and I will try to post more details in future.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)