Share via


Defining Relationships

This lesson shows how to define a relationship between data entities in a LightSwitch application.

Define a Relationship

Many applications use information from more than one source of data. For example, a retail order entry application might pull product information directly from the manufacturer’s database. By using LightSwitch, you can define a relationship between entities in different data sources as long as data in a field in one source is related to data in a field in the other source.

To connect to another data source

  1. In Solution Explorer, select the Data Sources node.

  2. On the Project menu, click Add Data Source.

  3. In the Attach Data Source wizard, click Database and then click Next.

  4. In the Connection Properties dialog box, enter the values in the following table, and then click OK.

    Field

    Value

    Server Name

    .\SQLExpress

    Log on to the server

    Use Windows Authentication

    Select or enter a database name

    Northwind

  5. On the Choose your Database Objects wizard page, expand the Tables node.

  6. Select the Products table and then click Finish.

    A new NorthwindData1 data source is added to the project and an entity is created for Products.

To add a relationship

  1. On the Data Designer toolbar, click Relationship.

  2. In the Add New Relationship dialog box, in the To column, in the Name: row, select Order_Detail.

  3. In the From column in the Multiplicity row, select Zero or one.

  4. In the To column in the Multiplicity row, select Many.

  5. In the Select the fields on ‘Product’ are shared with ‘Order_Detail’. section, in the Order_Detail (Foreign) column, select ProductID (Integer), and then click OK.

    A relationship between Order_Detail and Product

    A relationship is created between the Products entity and the Order_Details entity. If the ProductID for a record in the Order_Details table matches a ProductID in the Products table, information about the product can be displayed on a screen.

Closer Look

This lesson showed how to add a relationship between entities from different data sources. You may have noticed that the relationship is denoted in the designer by a dotted line. The dotted line signifies that the relationship is between different data sources. Relationships between entities in the same data source are denoted by a solid line.

When you selected the property that is shared by both entities, you may have noticed that some of the available properties showed a data type followed by a question mark, for example, UnitPrice (Decimal?). The question mark indicates that this is not a required field. Only required fields can be used to define a relationship.

This example used a second data connection to the Northwind database to illustrate how to add another data source. However, both of these data sources are accessing the same database. In a real application, you would have just added the Products table to your NorthwindData data source, and a Products entity would have been automatically generated.

You can also define relationships between disparate types of data sources. For example, you can define a relationship between a data entity that you create in LightSwitch and a SharePoint list.

Next Steps

This lesson is the last one in the Data section of the guided tour. The next set of lessons, Creating Screens, teaches how to create screens to display and edit data.

See Also

Other Resources

Working with Data in LightSwitch (Guided Tour)

How to: Create a Relationship Between Entities