Data: The Information Behind Your Application

In LightSwitch, you can connect to existing data or to data tables that you create, and you can define relationships between data sources and specify how the data is handled.

To provide data to a LightSwitch application, you can connect to an existing data source such as a SQL Server database, or you can create tables in the SQL Server Express database that is included in LightSwitch.

By using the data designer, you can adjust data settings or create relationships between data sources. LightSwitch handles many typical data management tasks such as field validation, transaction processing, and concurrency conflict resolution for you. You can customize these tasks by modifying properties in the Properties window, and you can write code to override or extend them.

Connecting to Data

You can connect to data from a SQL Server database, a SharePoint list, or a Windows Communication Foundation (WCF) Rich Internet Application (RIA) service. When you establish a connection, you can select specific tables, lists, or items. For every one of these, LightSwitch adds an entity to your solution. An entity is a basic unit of data that you can use in LightSwitch to assemble an application.

The following illustration shows the entities that LightSwitch has generated for a SQL Server database, a SharePoint list, and a WCF service.

Entities for each data source

For more information about how to connect to data, see How to: Connect to Data.

For more information about data access and storage in LightSwitch, see The Anatomy of a LightSwitch Application Part 4 – Data Access and Storage on the MSDN website.

Using the LightSwitch Internal Database

LightSwitch uses an internal database to store configuration settings such as security-related settings. You can add tables to this database and use them in your application just as you use entities from other data sources. The internal database is a great location to store your custom configuration information.

The following illustration shows that LightSwitch internal database as it appears in Solution Explorer.

Intrinsic database

For more information, see How to: Add a Table to the LightSwitch Internal Database.

Modifying Entities and Tables

You can use the data designer to add new fields, or change field names, field types, and field properties. The kinds of changes that you can make depend on whether you open a table from the internal database or an entity from an external data source.

If you open a table from the LightSwitch internal database, you can modify all fields and their properties. However, you can modify entities from external data sources only in ways that do not affect the data definition on the server. For example, you cannot change the type of a field from string to integer. If you want to modify external entities, change field types on the server and then refresh the data connection in LightSwitch.

For more information about how to refresh a connection to a data source, see How to: Connect to Data.

For more information about how to modify the fields of an entity or table, see How to: Define Data Fields.

Defining Relationships

You can use the data designer to define relationships between entities or tables. Relationships help you create screens in which the related data work together. You can also reference a relationship in your custom code because LightSwitch generates properties that represent the relationship. These properties appear in IntelliSense as you write code.

The following illustration shows a relationship between the Product entity and the Order_Detail entity.

A relationship between Order_Detail and Product

The kind of relationship that you can define or modify depends on the entities or tables that you include in the relationship. For example, you cannot use the data designer to define relationships between entities that are generated from the same external data source. In this case, LightSwitch imports the relationships that are already in the data source. If you want to change these relationships, you must change them on the server, and then refresh the data connection in LightSwitch. However, you can use the data designer to create relationships between entities from different data sources. For example, you can create a relationship between an entity from a SQL Server database and an entity from a SharePoint list.

For more information about how to create or modify data relationships, see How to: Define Data Relationships.

Validating Fields

LightSwitch automatically validates the fields of an entity or table based on rules that you define. An update cannot be committed to the data source until it complies with the validation rules. If a user adds data that does not comply, an error message is displayed.

The following illustration shows a validation error message.

Validation error message

LightSwitch provides several built-in validation rules that you can configure and apply to an entity or table without writing any code. You can also define custom validation rules by writing code.

For more information, see How to: Validate Data.

LightSwitch automatically handles data-related tasks such as transaction processing, concurrency events, and validation. However, you can override the default behavior or add code that extends it.

LightSwitch provides a strongly typed object model that you can use to perform data-management tasks. You can also add code to methods that are called when certain events occur, for example, when a user adds or deletes data.

For more information about tasks that you can perform by using code, see Performing Data-Related Tasks by Using Code.

For more information about the methods that LightSwitch calls when certain events occur, see How to: Handle Data Events.

For general information about how to write code in LightSwitch, see Writing Code in LightSwitch.

Title

Description

How to: Connect to Data

Describes how to consume data from a SQL Server database, a SharePoint list, or a WCF RIA service to a LightSwitch application.

Guidelines for Creating WCF RIA Services for LightSwitch

Provides information about how to store and retrieve connection strings as well as the effect that domain service class attributes have in a LightSwitch application.

How to: Add a Table to the LightSwitch Internal Database

Describes how to add data the SQL Server Express database that is included with LightSwitch..

How to: Define Data Fields

Describes how to add new fields (also known as properties) to an existing entity, and how to modify existing fields.

How to: Add a Computed Field

Describes how to create a field whose value is derived from other fields or from calculations made in business logic.

How to: Create a Drop-Down List of Values for a Field

Describes how to define a choice list for a field in an entity if that field can only contain certain values or if you want to make it easy to populate.

How to: Define Data Relationships

Describes how to create a relationship between entities or tables.

How to: Validate Data

Describes how to validate input by using custom or predefined validation rules.

How to: Handle Data Events

Describes how to write code that is executed when a data-related event occurs.

Working with Data-Related Objects in Code

Describes the runtime object model that you can use to perform data-related tasks in code.

Performing Data-Related Tasks by Using Code

Describes several data-related tasks that you can accomplish only by adding code to your application.

Reference: Data Designer Properties

Describes how you can modify the appearance and behavior of entities, tables, and fields by setting properties in the Properties window.

The Anatomy of a LightSwitch Application Part 4 – Data Access and Storage

Provides detailed information about data access and storage in a LightSwitch application.