Updating Data in a Silverlight Application: EF
Sat, Feb 5 2011 14:16
This prior post covered the details of setting up a database, defining an entity model using Entity Framework, building WCF RIA Services to access that entity model, and hooking it all up to a Silverlight DataGrid. That is all well and good. But most line of business (LOB) applications also need to update data. That's the purpose of this post.
NOTE: This post is part of a series that starts with this prior post. The example in this post uses the application that is built as part of that series.
NOTE: This post assumes you already have the database, entity model, and WCF RIA Service classes in place. If you need information on how to perform these prerequisite steps, see this prior post.
The first step in adding update functionality is to modify the UI. Add a Save button to a page containing a DataGrid.
The xaml for the Save button looks like this:
Then add the following to the code behind:
private void SaveButton_Click(object sender, RoutedEventArgs e)
Private Sub SaveButton_Click(ByVal sender As System.Object,
ByVal e As System.Windows.RoutedEventArgs) _
This code first completes any current edit operation on the grid. Then it uses the DomainDataSource that was defined in the xaml to submit the changes to the database.
When you run the application, the result will look something like this:
Edit the data in the grid and click Save to save the changes.
If you are happy with using a DomainDataSource and with saving the data in the code behind, you are done. If you want to handle the data retrieval without a DomainDataSource and follow a more MVVM approach, you still have some work to do. But that is another post.
Click here to download the associated source code.
EDIT 2/8/2011: Added the link to the source code.