Saving Data Across One-to-One Relationships

I’ve had a couple questions recently on how you could add data to tables that have a one-to-zero-or-one relationship in Visual Studio LightSwitch. Depending on whether the related record is mandatory (required) or not you have a couple different options when inserting data. So in this post I’ll show you a couple different options for doing that in both the desktop and mobile clients.

Our Data Model

Let’s take a simple data model of Customer and a related table CustomerNote. A customer can have zero or one note as defined by the relationship.

image

When we create our Customer screens, LightSwitch will automatically bring the CustomerNote fields in as well. For edit screens this is exactly what we want because this allows the user to edit fields across both tables on one screen. However, on AddNew screens, LightSwitch will not automatically add a new record to the CustomerNote table – we need to write code for that depending on if the note is mandatory or not.

image

Adding Code to the Silverlight Client

In order to support this in the Silverlight desktop client we need to write some VB (or C#) code.

Mandatory: Always add a related record

If you want to always add a related record, use the _Created event. Open the data designer to the parent record (in my case Customer), select the DesktopClient perspective and then write code in the _Created event to add the related record every time the parent is inserted.

image

Write the code in bold.

VB:

 Public Class Customer
    Private Sub Customer_Created()
Me.CustomerNote = New CustomerNote () 
    End Sub
End Class

C#:

 public partial class Customer
{
    partial void Customer_Created()
    {
this.CustomerNote = new CustomerNote (); 
    }
}

Now when we run the desktop client and add a new customer, the note fields are enabled.

image

Optional: Allow the user to decide

If the related record is optional and you want to have the user decide, add a gesture (like a button) to your screen. Open the screen designer and add a button, select “Write my own method” and call it AddNote, then edit the CanExecute and Execute methods.

image

VB:

 Private Sub AddNote_CanExecute(ByRef result As Boolean)
    'Only enable the button if there is no note
    result = (Me.CustomerProperty.CustomerNote Is Nothing) 
End Sub

Private Sub AddNote_Execute()
    ' Add the note
Me.CustomerProperty.CustomerNote = New CustomerNote() 
End Sub

C#:

 partial void AddNote_CanExecute(ref bool result)
{
    // Only enable the button if there is no note
result = (this.CustomerProperty.CustomerNote == null ); 
}

partial void AddNote_Execute()
{
    // Add the note
    this.CustomerProperty.CustomerNote = new CustomerNote (); 
}

 

Adding Code to the HTML Client

In order to support this in the html client we need to write some JavaScript code.

Mandatory: Always add a related record

Open the data designer to the parent record, but this time select the HTMLClient perspective. Then write JavaScript code in the _Created event to add the related record every time the parent is inserted.

 myapp.Customer.created = function (entity) {

    entity.CustomerNote =  new myapp.CustomerNote(); 
};

Notice the use of the myapp object here. You can use the myapp object in a variety of ways in order to access data and screens in the HTML client. Check out some other uses here- How to: Modify an HTML Screen by Using Code

Now when we run the HTML client and add a new customer, the note fields are enabled.

image

Optional: Allow the user to decide

Open the screen designer and add a button, select “Write my own method” and call it AddNote, then edit the CanExecute and Execute methods. Then write this code:

 myapp.AddEditCustomer.AddNote_canExecute = function (screen) {
    // enables the button (returns true) if the note doesn't exist
return (screen.Customer.CustomerNote == null ); 

};
myapp.AddEditCustomer.AddNote_execute = function (screen) {
    //Add a new note
screen.Customer.CustomerNote = new myapp.CustomerNote(); 
};

Wrap Up

So those are a couple different ways you can manage data participating in one-to-zero-or-one relationships. Keep in mind that these tables are in the same data source, but they don’t have to be, you can set up virtual relationships this way as well. If you do have separate data sources, however, you’ll also need to tell LightSwitch the order in which you want to update them. For more information on that see:Customize the Save command to save to multiple data sources

Enjoy!