This article will demonstrate how to build a Silverlight 4 application that uses (Entity Framework) Complex Types at the client level (through WCF services).
Before Silverlight 4, it wasn't possible to use complex types on the client, but now, Silverlight 4 can use RIA services to access (Complex) types that the Entity Framework creates. In previous versions of Silverlight, it was possible to access simple entity types but not complex types.
Using the code
Setting up the sample database
If you do not have Microsoft's Northwind database installed on your database server, you can download the script here (download script - 238 KB). Log into your SQL Server Management Studio and open a new query analyzer window, and run the unzipped script to create the database (including data) that is used in the Data Access Layer (DAL) within the overall project.
Create a new (empty) project and add three solution folders, for the data access layer, WCF service, and finally the Silverlight client.
Creating the Data Access Layer (Entity Model) project
Create the data access layer DLL by adding a new Class Library to the 'data access layer' solution in the project.
Add an entity framework model to the DAL:
Select the Stored Procedure 'Employee Sales by Country' to the entity model. This Stored Procedure will be used as it will return a complex type (fields from multiple tables).
Bring up the 'Model View' browser so that we can then easily navigate to the entity model.
Create\import the C# function that will represent the method to call the entity method in the DAL.
Create the complex type (class) for the Stored Procedure.
Your DAL project structure should look like this:
Creating the WCF Service project
Add a new WCF service to the WCF solution folder in the project.
Add a reference to the data access layer project in your references:
That is your newly added DAL project in your WCF project.
Add the following code to the
public interface IOrdersService
GetOrderDetailsData(DateTime start, DateTime end);
Add the following code to the
public class OrdersService : IOrdersService
private OrdersController orderCtrl;
orderCtrl = new OrdersController();
GetOrderDetailsData(DateTime start, DateTime end)
return this.orderCtrl.GetOrderDetails(start, end);
Compile the service before adding it to the Silverlight project below.
Creating the Silverlight 4 Client project
Create a new Silverlight project:
Enable the Silverlight project for RIA services and assign it the OrdersService service.
Add a reference to the WCF service:
The Silverlight project after adding the service reference:
Add the following code to the code-behind for the MainPage.cs. Notice that in the method
SaveScheduleCompleted, there is some LINQ performed on the collection of (entity class) '
Employee_Sales_By_Country_Result'. Thus we are able to create a class in the Entity Framework and also be able to use it in the client.
public partial class MainPage : UserControl
private void LoadOrderDetails()
DateTime start = DateTime.Parse("1997-01-01");
DateTime end = DateTime.Parse("1997-01-31");
OrdersServiceReference.OrdersServiceClient ordersService =
catch (FaultException fe)
catch (CommunicationException commProblem)
if (commProblem.InnerException is FaultException)
Console.WriteLine("An unknown exception was received. " +
Console.WriteLine("There was a communication problem. " +
private void SaveScheduleCompleted(object sender,
this.personDataGrid.ItemsSource = e.Result.Where(ord=>ord.SaleAmount>2000);
Add the following XAML code to the
MainPage (a simple grid to bind\display order results):
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="true" VerticalAlignment="Center"
Margin="57,66,0,0" Name="personDataGrid" Width="auto" />
Here is the overall project structure:
Points of Interest
Just as a side note, I found it easier to bind the Silverlight application to the WCF service (enabling RIA services), by designing the data access layer, WCF service, and then creating the Silverlight application - I found that creating the Silverlight application first with no RIA service to bind to (but still enabling RIA services) meant that it didn't pick up the services when I later enabled them.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.