Submitting Only Selected Entities in Silverlight

Imagine that you have a group of contacts of many friends but you want to send messages to a subgroup of that group. 

While dealing with data in Silverlight, you have entities, you have Domain Context, you have a lot of stuff to carry on with. Sometimes there are so many entities that have changes in them and you want to submit the changes of any selected entity instead of all, what you do is you reject the changes of the entity you want to not submit. To demonstrate this, let's go through with a demo.

As you know, we always start from scratch, so let's create a new Silverlight project, RejectChanges. Also create a databaseTestingEntities, having two tables, Location and Food. Here is my tables structure:



Now in the Silverlight project add an EntityDataModel based on the database created above. You can follow the step mentioned in Creating Entity Data Model from a database. Once you have created the model, you need to create a domain service basing it on the model that you have added recently. The procedure for creating a domain service class you can get them from here. All set up now. We are ready to go.

As far as design is concerned, in mainpage.xaml I have added a few textboxes. The XAML of which is below:

<UserControl x:Class="RejectChanges.MainPage"
 d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock HorizontalAlignment="Left" Margin="51,70,0,0" TextWrapping="Wrap" Text="City" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" x:Name="txtCity" Height="23" Margin="51,86,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="200"/>
        <TextBlock HorizontalAlignment="Left" Margin="51,114,0,0" TextWrapping="Wrap" Text="Speciality" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" x:Name="txtSpeciality" Height="23" Margin="51,130,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="200"/>
        <TextBlock HorizontalAlignment="Left" Margin="51,163,0,0" TextWrapping="Wrap" Text="Address" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" x:Name="txtAddress" Height="50" Margin="51,179,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="200"/>
        <Button Content="SubmitAll" HorizontalAlignment="Left" Margin="223,242,0,0" VerticalAlignment="Top" Width="93" Click="SaveAll_Click"/>
        <Button Content="Location Only" HorizontalAlignment="Left" Margin="124,242,0,0" VerticalAlignment="Top" Width="84" Click="LocationOnly_Click"/>
        <Button Content="Speciality Only" HorizontalAlignment="Left" Margin="15,242,0,0" VerticalAlignment="Top" Width="99" Click="SpecialityOnly_Click"/>

To keep the concept vivid and distinct, I have added three buttons, SubmitAll, Location Only and Specialty Only. While SubmitAllwill submit all the changes, Location Only and Specialty Only will submit the changes to the respective entities mentioned in their names. Awesome until now? Next we are going to write some code, some C#.

Here is the entire code of mainpage.cs that I have:

public partial class MainPage : UserControl
RejectDomainContext _context = new RejectDomainContext(); 
public MainPage()
private void AddItemsToContext()
var city = txtCity.Text.Trim();
var speciality = txtSpeciality.Text.Trim();
var address = txtAddress.Text.Trim();
var foodEn = new Food();
var locationEn = new Location();
        locationEn.City = city;
        locationEn.Address = address;
        foodEn.Speciality = speciality;
private void SubmitOnlyFood()
private void SubmitOnlyLocation()
private void SubmitAll()
private void SaveAll_Click(object sender, RoutedEventArgs e)
private void SpecialityOnly_Click(object sender, RoutedEventArgs e)
private void LocationOnly_Click(object sender, RoutedEventArgs e)

The entire code is self explanatory, the only functions of interest are SubmitOnlyFood() and SubmitOnlyLocation(). IRevertibleChangeTracking is an interface that tracks whether the mentioned entity has any changes or not. It possess the two methods:

  • AcceptChanges
  • RejectChanges

It is clear from the name itself what each method does. In the two functions above, what we are doing is keeping only the changes of the entity of interest and rejecting the changes from the other. In this way when we commit a submit change operation in the domain context, the entity whose changes are not rejected are submitted.

So it is possible to submit entity specific changes.

Happy Reading!!!