Elucidate LightSwitch Beginning in Visual Studio 2013 Part 2

Welcome to Part 2 of the Elucidate LightSwitch article series.
In this article we will see a walkthrough of LightSwitch. We will have a look at one of the most important building blocks of a LightSwitch application, how organize data in columns and rows in a table. We will see how to create and work with screens in LightSwitch.
Previous articles have provided an introduction to LightSwitch and the creation of the contact entity. You can get them from the following:

Now let's continue with the next steps.
Step 1:  Open the Properties window and select a property on the entity and we will see the related settings we can configure for it depending on the perspective as indicated at the bottom of the designer. The Server perspective allows us to configure the storage and validation properties as well as the default display name of the field. 
We will see various settings depending on the type of data we chose for the property. All properties have an “Appearance” section in the property window that allow you to specify the Display Name that will appear in field labels on screens in the application. By default, if you use Upper Camel Case (a.k.a Pascal case) for your entity property names then LightSwitch will put a space between the phrases. For instance, the Display Name for the “LastName” property will become “Last Name” automatically. So it's best practice to use this casing for your entity properties.
Now let's set a few additional settings for the Contact entity. First, select the Id field and in the Appearance section, uncheck “Display by default”. This makes that the property doesn't show up anywhere in the user interface. As mentioned earlier, the primary key is an internal field used to locate a row in the table and isn't modifiable so the user does not need to see it on any screens in the application. 
For BirthDate, set the minimum value to 1/1/1980 so that users can't enter dates before that.
We can also set a maximum value here, but that would hard-code a static value in the validation check. Click on the “Custom Validation” link in the properties window and provide the code to do the check. This check always runs on the server whenever a contact is being saved. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Microsoft.LightSwitch;  
  6. namespace LightSwitchApplication  
  7. {  
  8.     public partial class Contact  
  9.     {  
  10.         partial void BirthDate_Validate(EntityValidationResultsBuilder results)  
  11.         {  
  12.             if (this.BirthDate.HasValue && this.BirthDate > DateTime.Today)  
  13.             {  
  14.                 results.AddPropertyError("Birthdate cannot be in the future.");  
  15.             }  
  17.         }  
  18.     }  
  19. }  
For Gender, we want to display a fixed set of static values to the user: “Female”,“Male”. In order to do this in LightSwitch we can use a Choice List. A Choice List is appropriate for choices that are always static and relatively small, like gender in this case or “Yes/No” values. If your list of choices is dynamic (or is a very large list) then you should create a table to hold the lookup values and then relate that to your master table via a many-to-one relationship. This will cause LightSwitch to automatically create a picker list for you on screens.
Click on “Choice List…” on the Properties window and this will open a window that will let you define the values that are stored in the table and the display name you want the user to see. For our purposes, we just want to store an “F” or “M'” in the underlying database table. Therefore, also set the Maximum Length to 1.

In this article we saw how to create a contact entity in  LightSwitch. In future articles we will test the contact entity.

Similar Articles