Adding a Computed Field in Database Using LightSwitch Visual Studio 2012

Adding a Computed Field to the LightSwitch Database

We can easily create new fields that derive their values from the values of other fields stored in a database for a LightSwitch application in Visual Studio 2012.

We can't include a computed field as part of a filter condition or sort term in a query. Also, we can't sort information in a screen by choosing the column heading of a computed field.

Step 1

Open the Solution Explorer.

Image 1


Step 2

In the Solution Explorer, double-click the table or right-click on it and select "Table" to open it.

Image 2


Step 3

The Table is shown in the Data Designer.

Image 3


Step 4

Choose the Computed Property option from the Command Bar in the Data Designer.

Image 4


Step 5

We will see that a new field is shown in the bottom row of the table.

Image 5


Step 6

In the Name column enter the Name (Total) and similarly in the Type column enter the Type (Decimal) for the new field.

Image 6


Step 7

Open the Property Window and choose the edit method link.

Image 7


Step 8

The Code Editor opens and generates a method named "Total_Compute".

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.LightSwitch;

namespace LightSwitchApplication


    public partial class Emp1Tables



        partial void Total_Compute(ref decimal result)


            // Set result to the desired field value

            result = this.Quantity * this.Price;




Up Next
    Ebook Download
    View all
    View all