📒
OpenRiaServices
  • Start
  • Prerequisites for Open Ria Services
    • Walkthrough: Installing and Configuring SQL Server 2008 R2 Express with Advanced Services
    • Walkthrough: Installing the AdventureWorks OLTP and LT sample databases
  • Creating Open Ria Services Solutions
    • Walkthrough: Taking a Tour of Open Ria Services
    • Walkthrough: Creating a Open Ria Services Solution
    • Walkthrough: Creating a Open Ria Service with the Code First Approach
    • Walkthrough: Using the Silverlight Business Application Template
    • Walkthrough: Creating a Open Ria Services Class Library
    • Walkthrough: Localizing a Business Application
    • How to: Create a Domain Service that uses POCO-defined Entities
    • How to: Add or Remove a Open Ria Services Link
    • Using the Domain Service Wizard
  • Building Secure Applications with Open Ria Services
  • Deploying and Localizing a Open Ria Services Solutions
    • Troubleshooting the Deployment of a Open Ria Services Solution
    • Troubleshooting the Deployment of a Open Ria Services Solution
    • Walkthrough: Localizing a Business Application
  • Middle Tier
    • Domain Services
      • Walkthrough: Adding Query Methods
      • How to: Add Business Logic to the Domain Service
      • How to: Create a Domain Service that uses POCO-defined Entities
      • How to: Use HTTPS with a Domain Service
    • Data
      • Compositional Hierarchies
      • Presentation Models
      • Inheritance in Data Models
      • Complex Types
      • Shared Entities
      • Walkthrough: Sharing Entities between Multiple Domain Services
      • How to: Add Metadata Classes
      • How to: Validate Data
      • Managing Data Concurrency
    • Shared Code
      • How to: Share Code through Source Files
      • Walkthrough: Creating a Open Ria Services Class Library
  • Silverlight Clients
    • Client Code Generation
    • DomainContext and Operations
    • DomainDataSource
    • Error Handling on the Client
    • Customizing Generated Code
      • How to: Add Computed Properties on the Client
  • Accessing non-Silverlight Clients
    • ASP.NET Clients
    • Walkthrough: Using the Domain Service in ASP.NET Applications
  • Authentication, Roles, and Profiles
    • How to: Enable Authentication in Open Ria Services
    • How to: Enable Roles in Open Ria Services
    • How to: Enable Profiles in Open Ria Services
    • How to: Create a Custom Authorization Attribute
    • Walkthrough: Using Authentication Service with Silverlight Business Application
    • Walkthrough: Using Authentication Service with Silverlight Navigation Application
  • End-to-EndScenarios
    • Walkthrough: Retrieving and Displaying Data From a Domain Service
    • Walkthrough: Editing Data From a Domain Service
    • Walkthrough: Displaying Data in a Silverlight Business Application
    • Walkthrough: Displaying Related Data in a Silverlight Business Application
Powered by GitBook
On this page
  • Prerequisites
  • To display and edit data from a domain service
  • To set metadata for the entity to update

Was this helpful?

  1. End-to-EndScenarios

Walkthrough: Editing Data From a Domain Service

PreviousWalkthrough: Retrieving and Displaying Data From a Domain ServiceNextWalkthrough: Displaying Data in a Silverlight Business Application

Last updated 4 years ago

Was this helpful?

[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see or 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 object, and the changes are submitted collectively when you call the 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 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 and continues from the application created with the procedures described there.

To display and edit data from a domain service

  1. Open the RIAServicesExample solution from .

  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.

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel HorizontalAlignment="Center" Grid.Row="0" Orientation="Horizontal">
            <Button Content="Save Changes" Click="SaveButton_Click" Margin="5" x:Name="SaveButton"></Button>
            <Button Content="Reject Changes" Click="RejectButton_Click" Margin="5" x:Name="RejectButton"></Button>
            <TextBlock x:Name="ChangeText" VerticalAlignment="Center" Width="Auto"></TextBlock>
        </StackPanel>
        <data:DataGrid Grid.Row="1" Name="CustomerGrid" RowEditEnded="CustomerGrid_RowEditEnded"></data:DataGrid>
    </Grid>
  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.

    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        _customerContext.SubmitChanges(AddressOf OnSubmitCompleted, Nothing)
    End Sub
    
    Private Sub RejectButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        _customerContext.RejectChanges()
        CheckChanges()
    End Sub
    
    Private Sub CustomerGrid_RowEditEnded(ByVal sender As System.Object, ByVal e As System.Windows.Controls.DataGridRowEditEndedEventArgs)
        CheckChanges()
    End Sub
    
    Private Sub CheckChanges()
        Dim changeSet = _customerContext.EntityContainer.GetChanges()
        ChangeText.Text = changeSet.ToString()
    
        Dim hasChanges = _customerContext.HasChanges
        SaveButton.IsEnabled = hasChanges
        RejectButton.IsEnabled = hasChanges
    End Sub
    
    Private Sub OnSubmitCompleted(ByVal so As SubmitOperation)
        If (so.HasError) Then
            MessageBox.Show(String.Format("Submit Failed: {0}", so.Error.Message))
            so.MarkErrorAsHandled()
        End If
        CheckChanges()
    End Sub
    private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        _customerContext.SubmitChanges(OnSubmitCompleted, null);
    }
    
    private void RejectButton_Click(object sender, RoutedEventArgs e)
    {
        _customerContext.RejectChanges();
        CheckChanges();
    }
    
    private void CustomerGrid_RowEditEnded(object sender, DataGridRowEditEndedEventArgs e)
    {
        CheckChanges();
    }
    
    private void CheckChanges()
    {
        EntityChangeSet changeSet = _customerContext.EntityContainer.GetChanges();
        ChangeText.Text = changeSet.ToString();
    
        bool hasChanges = _customerContext.HasChanges;
        SaveButton.IsEnabled = hasChanges;
        RejectButton.IsEnabled = hasChanges;
    }
    
    private void OnSubmitCompleted(SubmitOperation so)
    {
        if (so.HasError)
        {
            MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
            so.MarkErrorAsHandled();
        }
        CheckChanges();
    }

To set metadata for the entity to update

  1. In the server project, open the metadata class named Customer.metadata.cs or Customer.metadata.vb.

  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. The following shows the contents of the metadata class.

    ``` vb

    _ Partial Public Class Customer

    Friend NotInheritable Class CustomerMetadata
    
        'Metadata classes are not meant to be instantiated.
        Private Sub New()
            MyBase.New
        End Sub
    
        <Editable(False)> _
        Public CustomerID As Integer
    
        <Editable(False)> _
        Public ModifiedDate As DateTime
    
        <Exclude()> _
        Public rowguid As Guid
    End Class
End Class
```

``` csharp
[MetadataTypeAttribute(typeof(Customer.CustomerMetadata))]
public partial class Customer
{
    internal sealed class CustomerMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private CustomerMetadata()
        {
        }

        [Editable(false)]
        public int CustomerID;

        [Editable(false)]
        public DateTime ModifiedDate;

        [Exclude]
        public Guid rowguid;

    }
}
```
  1. 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.

For more information, see .

Add the attribute to the rowguid property.

You apply the 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.

Add the required using or Imports statements for namespaces, such as and .

Release Notes
Changelog
EntityChangeSet
SubmitChanges
Prerequisites for Open Ria Services
Walkthrough: Creating a Open Ria Services Solution
Walkthrough: Creating a Open Ria Services Solution
How to: Add Metadata Classes
ExcludeAttribute
ExcludeAttribute
System.ComponentModel.DataAnnotations
OpenRiaServices.Server