Walkthrough: Editing Data From a Domain Service
[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see Release Notes or Changelog for a list of changes since WCF RIA Services ]
In this walkthrough, you learn how to create an interface that enables the user to change the displayed data and to save those changes in the databases.
When you have added update, insert, or delete methods in a domain service, you can create an interface in a Silverlight client that enables users to change the data. All of the changes are tracked in an EntityChangeSet object, and the changes are submitted collectively when you call the SubmitChanges method.

Prerequisites

This and the other walkthroughs presented in the Open Ria Services documentation require several prerequisite programs, such as Visual Studio and the Silverlight Developer Runtime and SDK, be installed and configured properly, in addition to Open Ria Services and the Open 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 Open 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 Open Ria Services walkthroughs.
This walkthrough assumes that you have completed the Walkthrough: Creating a Open Ria Services Solution and continues from the application created with the procedures described there.

To display and edit data from a domain service

  1. 1.
    Open the RIAServicesExample solution from Walkthrough: Creating a Open Ria Services Solution.
  2. 2.
    In MainPage.xaml, change the interface to enable the user to save or reject changes in the DataGrid.
    The following XAML adds a Save Changes button, a Reject Changes button, and a text block.
    1
    <Grid x:Name="LayoutRoot" Background="White">
    2
    <Grid.RowDefinitions>
    3
    <RowDefinition Height="40"></RowDefinition>
    4
    <RowDefinition Height="*"></RowDefinition>
    5
    </Grid.RowDefinitions>
    6
    <StackPanel HorizontalAlignment="Center" Grid.Row="0" Orientation="Horizontal">
    7
    <Button Content="Save Changes" Click="SaveButton_Click" Margin="5" x:Name="SaveButton"></Button>
    8
    <Button Content="Reject Changes" Click="RejectButton_Click" Margin="5" x:Name="RejectButton"></Button>
    9
    <TextBlock x:Name="ChangeText" VerticalAlignment="Center" Width="Auto"></TextBlock>
    10
    </StackPanel>
    11
    <data:DataGrid Grid.Row="1" Name="CustomerGrid" RowEditEnded="CustomerGrid_RowEditEnded"></data:DataGrid>
    12
    </Grid>
    Copied!
  3. 3.
    In the code-behind page for MainPage.xaml, add event handlers for the button click events, an event handler for the RowEditEnded event, a callback method named OnSubmitCompleted, and a method that evaluates the pending changes.
    1
    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    2
    _customerContext.SubmitChanges(AddressOf OnSubmitCompleted, Nothing)
    3
    End Sub
    4
    ​
    5
    Private Sub RejectButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    6
    _customerContext.RejectChanges()
    7
    CheckChanges()
    8
    End Sub
    9
    ​
    10
    Private Sub CustomerGrid_RowEditEnded(ByVal sender As System.Object, ByVal e As System.Windows.Controls.DataGridRowEditEndedEventArgs)
    11
    CheckChanges()
    12
    End Sub
    13
    ​
    14
    Private Sub CheckChanges()
    15
    Dim changeSet = _customerContext.EntityContainer.GetChanges()
    16
    ChangeText.Text = changeSet.ToString()
    17
    ​
    18
    Dim hasChanges = _customerContext.HasChanges
    19
    SaveButton.IsEnabled = hasChanges
    20
    RejectButton.IsEnabled = hasChanges
    21
    End Sub
    22
    ​
    23
    Private Sub OnSubmitCompleted(ByVal so As SubmitOperation)
    24
    If (so.HasError) Then
    25
    MessageBox.Show(String.Format("Submit Failed: {0}", so.Error.Message))
    26
    so.MarkErrorAsHandled()
    27
    End If
    28
    CheckChanges()
    29
    End Sub
    Copied!
    1
    private void SaveButton_Click(object sender, RoutedEventArgs e)
    2
    {
    3
    _customerContext.SubmitChanges(OnSubmitCompleted, null);
    4
    }
    5
    ​
    6
    private void RejectButton_Click(object sender, RoutedEventArgs e)
    7
    {
    8
    _customerContext.RejectChanges();
    9
    CheckChanges();
    10
    }
    11
    ​
    12
    private void CustomerGrid_RowEditEnded(object sender, DataGridRowEditEndedEventArgs e)
    13
    {
    14
    CheckChanges();
    15
    }
    16
    ​
    17
    private void CheckChanges()
    18
    {
    19
    EntityChangeSet changeSet = _customerContext.EntityContainer.GetChanges();
    20
    ChangeText.Text = changeSet.ToString();
    21
    ​
    22
    bool hasChanges = _customerContext.HasChanges;
    23
    SaveButton.IsEnabled = hasChanges;
    24
    RejectButton.IsEnabled = hasChanges;
    25
    }
    26
    ​
    27
    private void OnSubmitCompleted(SubmitOperation so)
    28
    {
    29
    if (so.HasError)
    30
    {
    31
    MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
    32
    so.MarkErrorAsHandled();
    33
    }
    34
    CheckChanges();
    35
    }
    Copied!

To set metadata for the entity to update

  1. 1.
    In the server project, open the metadata class named Customer.metadata.cs or Customer.metadata.vb.
    For more information, see How to: Add Metadata Classes.
  2. 2.
    In the metadata class, add the EditableAttribute attribute with the AllowEdit property set to false to the CustomerID and ModifiedDate properties.
    You apply the EditableAttribute attribute to a property to indicate whether the property is intended for editing by a user in a client application. When you set the AllowEdit property to false, the property is read-only in the client application. In this example, you will display the values for the CustomerID and ModifiedDate properties but do not want the user to modify these values.
  3. 3.
    Add the ExcludeAttribute attribute to the rowguid property.
    You apply the ExcludeAttribute attribute to properties that you do not want to include in the generated code for the client project. In this example, there is no reason to expose the rowguid property in the client project.
    The following shows the contents of the metadata class.
    ``` vb
    _ Partial Public Class Customer
    1
    Friend NotInheritable Class CustomerMetadata
    2
    ​
    3
    'Metadata classes are not meant to be instantiated.
    4
    Private Sub New()
    5
    MyBase.New
    6
    End Sub
    7
    ​
    8
    <Editable(False)> _
    9
    Public CustomerID As Integer
    10
    ​
    11
    <Editable(False)> _
    12
    Public ModifiedDate As DateTime
    13
    ​
    14
    <Exclude()> _
    15
    Public rowguid As Guid
    Copied!
1
End Class
2
End Class
3
```
4
​
5
``` csharp
6
[MetadataTypeAttribute(typeof(Customer.CustomerMetadata))]
7
public partial class Customer
8
{
9
internal sealed class CustomerMetadata
10
{
11
​
12
// Metadata classes are not meant to be instantiated.
13
private CustomerMetadata()
14
{
15
}
16
​
17
[Editable(false)]
18
public int CustomerID;
19
​
20
[Editable(false)]
21
public DateTime ModifiedDate;
22
​
23
[Exclude]
24
public Guid rowguid;
25
​
26
}
27
}
28
```
Copied!
  1. 1.
    Add the required using or Imports statements for namespaces, such as System.ComponentModel.DataAnnotations and OpenRiaServices.Server.
  2. 2.
    Run (F5) the application.
    Notice that you can edit the values in the DataGrid. When you exit the row you have edited, the ChangeText value is updated with a description of the pending changes. When you click the Save Changes button, the data changes are saved in the database. When you click the Reject Changes button, all pending changes are reverted.
Last modified 6mo ago