Walkthrough: Displaying Related Data in a Silverlight Business Application
[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]
In this walkthrough, you create a Silverlight Business Application that displays data from two related tables in the AdventureWorks sample database.
This walkthrough illustrates the following tasks:
Creating a Silverlight Business Application that consists of two projects: a Silverlight client and an ASP.NET Web Application.
Changing the application name by modifying a resource string.
Creating an entity data model based on a database file.
Creating a domain service that exposes the data in the entity data model to the Silverlight client. For more information, see Domain Services.
Modifying a query in the Domain Service to return related data.
Modifying the domain services metadata to support loading of related data.
Creating additional Silverlight pages to present data to users.
Adding buttons to the default navigation bar to access the Silverlight pages.
Configuring the Silverlight pages to display data by dragging items from the Data Sources window to the Silverlight Designer.
Adding a DataPager to navigate records.
Prerequisites
This and the other walkthroughs presented in the WCF RIA Services documentation require several prerequisite programs, such as Visual Studio 2010 and the Silverlight Developer Runtime and SDK, be installed and configured properly, in addition to WCF RIA Services and the WCF RIA Services Toolkit. They also require installing and configuring SQL Server 2008 R2 Express with Advanced Services and installing the AdventureWorks OLTP and LT database.
Detailed instructions for the satisfaction of each of these prerequisites are provided by the topics within the Prerequisites for WCF RIA Services node. Follow the instructions provided there before proceeding with this walkthrough to ensure that you encounter as few problems as possible when working through this RIA Services walkthroughs.
This walkthrough assumes that you can create a Silverlight Business Application. The procedure for completing this task is described in the Walkthrough: Using the Silverlight Business Application Template.
Creating the Silverlight Business Application
Silverlight Business Applications are solutions with two projects: a Silverlight application and an ASP.NET Web Application that hosts the Silverlight application. Silverlight Business Applications have built-in functionality. By default, they have a Home page, an About page, a navigation bar, and site registration functionality.
To create the application
In Visual Studio 2010, create a new Silverlight Business Application project in Visual Basic or C# named AdventureWorksOrders. The procedure for completing this task is described in the Walkthrough: Using the Silverlight Business Application Template.
The AdventureWorksOrders solution is created with two projects: an AdventureWorksOrders Silverlight project and an AdventureWorksOrders.Web Web application project.
In Solution Explorer, expand the AdventureWorksOrders project.
Expand the Assets folder and then expand Resources.
Double-click ApplicationStrings.resx to open the Resource Designer.
Change the ApplicationName resource string Value to Adventure Works Orders.
Save the changes and close the ApplicationStrings.resx file.
Run the application.
The home page opens and displays the default design, which includes the updated application name.
Creating a Data Model for the Application
To manage the data in the application, you use an entity data model.
To create an entity data model
In Solution Explorer, right-click AdventureWorksOrders.Web, click Add, and then click New Item.
The Add New Item dialog box appears.
In the Data category, click the ADO.NET Entity Data Model template.
Change the name to AdventureWorksEDM.edmx, and then click Add.
The Entity Data Model Wizard opens.
On the Choose Model Contents page, click Generate from database and then click Next.
On the Choose Your Data Connection page, click New Connection.
The Connection Properties dialog box appears.
Select Microsoft SQL Server Database File for the data source and specify the location of the AdventureWorksLT database file.
Click OK.
On the Choose Your Data Connection page, click Next.
If a message appears asking you if you want to copy the database file to your project and modify the connection, click Yes.
On the Choose Your Database Objects page, expand the Tables node.
Add check marks next to the SalesOrderDetail (SalesLT) and SalesOrderHeader (SalesLT) tables.
Click Finish.
The SalesOrderDetail and SalesOrderHeader tables appear in the Entity Designer.
Build the solution.
You must build the solution prior to adding a domain service.
Creating a Domain Service
A domain service exposes the data entities and operations in the data model to the client. In this procedure, you add a domain service to the server project.
To create a domain service
In Solution Explorer, right-click AdventureWorksOrders.Web, click Add, and then click New Item.
The Add New Item dialog box appears.
In the Web category, click the Domain Service Class template.
Name the Domain Service Class AdventureWorksService and then click Add.
The Add New Domain Service Class dialog box opens.
Select the Enable editing check boxes for both the SalesOrderDetail and SalesOrderHeader entities.
Click OK.
Build the solution.
Editing a Domain Service Query to Include Related Data
The domain service provides default operations that you should modify for your specific application. In this procedure, you edit the GetSalesOrderHeaders query so that it returns the related SalesOrderDetail records. Adding a sort order to the query is also required for the DataPager to run.
To change a Domain Service query
In Solution Explorer, double-click AdventureWorksService.vb or AdventureWorksService.cs.
Update the GetSalesOrderHeaders method as shown in the following code.
Public Function GetSalesOrderHeaders() As IQueryable(Of SalesOrderHeader) Return Me.ObjectContext.SalesOrderHeaders.Include("SalesOrderDetails").OrderBy(Function(c) c.SalesOrderID) End Function
public IQueryable<SalesOrderHeader> GetSalesOrderHeaders() { return this.ObjectContext.SalesOrderHeaders.Include("SalesOrderDetails").OrderBy(e => e.SalesOrderID); }
In Solution Explorer, double-click AdventureWorksService.metadata.vb or AdventureWorksService.metadata.cs.
In the SalesOrderHeaderMetadata class, add an Include attribute just before the SalesOrderDetails entity collection statement:
<Include()> Public Property SalesOrderDetails As EntityCollection(Of SalesOrderDetail)
[Include] public EntityCollection<SalesOrderDetail> SalesOrderDetails { get; set; }
Build the solution.
Creating a Silverlight Page to Display Data
In this procedure, you add an Orders page to display data from the tables.
To create a Silverlight page
In Solution Explorer, in AdventureWorksOrders, right-click the Views folder and add a new item.
In the Add New Item dialog box, click the Silverlight category and then click the Silverlight Page template.
Change the name to Orders.xaml, and then click Add.
Adding a Navigation Button to the Home page
In this procedure, you add a button to the application's home page that navigates to the Orders page.
To create a navigation button
In Solution Explorer, double-click MainPage.xaml.
In XAML view add the following code after the <HyperlinkButton x:Name="Link2" ... /> line.
<Rectangle x:Name="Divider3" Style="{StaticResource DividerStyle}"/> <HyperlinkButton x:Name="Link3" Content="Orders" Style="{StaticResource LinkStyle}" NavigateUri="/Orders" TargetName="ContentFrame"/>
Run the application.
The Orders button should appear in the navigation bar.
Click the Orders button.
The blank Orders page appears.
Displaying Related Data on the Page
In this procedure, you create data bound controls on the Orders page by dragging items from the Data Sources window directly to the designer. You first create a DataGrid that displays basic information about an individual order, such as Account Number, CustomerID, and Amount Due. Then you create a Grid control that lists the individual items in that order.
To create the data bound controls
In Solution Explorer, double-click Orders.xaml.
Click the Data menu and then click Show Data Sources.
The Data Sources window opens. Notice that the Data Sources window already contains the entities available to the application.
Click the SalesOrderHeader node and then click the drop-down menu.
Click Details.
From the Data Sources window, drag the SalesOrderHeader node to the designer.
A Grid is created that is populated with controls for the data fields from the SalesOrderHeader table.
In the Data Sources window, expand the SalesOrderHeader node.
Click the SalesOrderDetails node (the child node of the SalesOrderHeader node).
Drag the SalesOrderDetails node to the designer next to the individual order controls.
A DataGrid is created that is populated with fields from the SalesOrderDetail table.
Adding Paging to Navigate the Order Data
In this procedure, you add paging by adding a DataPager to the data source. You set the DataPager.Source to the DomainDataSource that manages the data the pager should control.
Note
The DataPager requires that its data source is explicitly sorted. That is why the OrderBy clause was added to the query earlier in this walkthrough.
To configure paging of the data
In Solution Explorer, double-click Orders.xaml.
Locate the DataPager control in the Toolbox.
Drag the DataPager to the designer.
In the Data Sources window, click the SalesOrderHeader node.
Drag the SalesOrderHeader node to the DataPager.
This sets the DataPager's Source property to the correct data source.
Select the DataPager on the designer.
In the Properties window, set the PageSize property to 1.
Testing the Application
In this procedure, you build and run the application to verify that the application behaves as expected.
To test the application
On the Build menu, click Build Solution.
Verify that the solution builds without errors.
Run the application.
Click the Orders button.
Click the navigation buttons on the DataPager buttons to navigate through order records.
Note that the related order details appear in the DataGrid when navigating through the order header records.
Next Steps
After completing this walkthrough, you can perform the following related tasks:
- Modify records and save changes back to the database. For more information, see Walkthrough: Editing Data From a Domain Service.