Bagikan melalui


Creating LINQ to SQL Classes: Using the O/R Designer

The Object Relational Designer (O/R Designer) enables mapping LINQ to SQL classes to tables in a database. These mapped classes are also known as entity classes. The properties of an entity class map to the columns of the table, and you can bind the data to controls on a Windows Form. To learn more about LINQ to SQL, see Using LINQ in Visual Basic Express.

In this lesson, you will learn how to use the O/R Designer to create entity classes that are mapped to tables in the Northwind database. You will then display data on a Windows Forms application.

Prerequisites

  • If you did not install SQL Server Express Edition when you installed Visual Basic Express, you must do this before continuing. SQL Server Compact 3.5 databases are not supported in LINQ to SQL. For information about how to obtain SQL Server Express Edition, see the Obtaining SQL Server Express Edition section in How to: Install Sample Databases.

  • To complete this lesson, you must have access to the SQL Server version of the Northwind sample database. For more information, see How to: Install Sample Databases.

Try it!

To open the O/R Designer

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Forms Application, name the project LINQToSQLSample, and then click OK.

    A new Windows Forms project opens.

  3. On the Project menu, click Add New Item.

  4. Click the LINQ to SQL Classes template and type NorthwindSample.dbml in the Name box.

  5. Click Add.

    An empty LINQ to SQL file (NorthwindSample.dbml) is added to the project, and the O/R Designer opens.

The O/R Designer is made up of two design surfaces: the Entities pane on the left, and the Methods pane area on the right. The Entities pane is the main design surface that displays the entity classes that are defined in the file. The Methods pane is the design surface that displays the DataContext methods that are mapped to stored procedures and functions. You can hide the Methods pane by right-clicking the O/R Designer and then clicking Hide Methods Pane. You will not use the Methods pane in this lesson, but you can learn more about it in DataContext Methods (O/R Designer).

Click the main design surface and verify that its name is NorthwindSampleDataContext by looking at the Name property in the Properties window. You can create entity classes by dragging database tables from the Database Explorer onto the O/R Designer surface. But first you must create a connection to the database. This process is like the process described in Getting the Information You Need: Connecting to an Existing Database, except that in this lesson you will use a SQL Server Express database.

To connect to the Northwind database

  1. On the View menu, click Database Explorer.

    The Database Explorer opens.

  2. Click the Connect to Database button in the Database Explorer toolbar.

  3. Click Microsoft SQL Server Database File in the Choose Data Sources window and then click Continue.

    The Add Connection dialog box opens.

  4. In the Add Connection dialog box, make sure that the data source is Microsoft SQL Server Database File (SqlClient).

  5. Click Browse and then locate and select the Northwind.mdf database. Click Open.

  6. Click Test Connection to verify that the connection succeeded.

  7. Click OK to close the Add Connection dialog box.

Creating Entity Classes

The tables in the Northwind database are now visible in the Database Explorer. You can drag tables from the Database Explorer onto the O/R Designer. This automatically creates entity classes that have properties that map to the columns in the tables that you dragged from the Database Explorer.

To create entity classes

  1. In the Database Explorer, expand Northwind.mdf, and then expand Tables.

  2. Drag Customers from Database Explorer onto the O/R Designer surface. If you are prompted to copy the data file to your project, click Yes.

    An entity class named Customer is created. It has properties that correspond to the columns in the Customers table.

  3. Drag Orders from Database Explorer onto the O/R Designer surface.

    An entity class named Order is created that has an association (relationship) to the Customer entity class. It has properties that correspond to the columns in the Orders table.

Now that you have created the entity classes, you can add them to the Data Sources window by running the Data Source Configuration Wizard.

To add an entity class as an object data source in the Data Sources window

  1. On the Build menu, click Build LINQToSQLSample.

  2. On the Data menu, click Show Data Sources.

  3. In the Data Sources window, click Add New Data Source.

  4. Click Object on the Choose a Data Source Type page and then click Next.

  5. Expand the LINQToSQLSample node and locate the Customer class.

    Note

    If the Customer class is not available, click Cancel, build the project, and run the wizard again.

  6. Click Finish to create the data source and add the Customer entity class to the Data Sources window.

Binding Controls on a Windows Form to Entity Classes

You can add controls that are data bound to entity classes to a Windows Form by dragging tables or fields from the Data Sources window to the form.

To add controls bound to the entity classes

  1. Right-click Form1 in Solution Explorer and then click View Designer.

  2. In the Data Sources window, expand Customer.

  3. Click the drop-down arrow next to CompanyName and then click Label.

  4. Drag CompanyName to the form.

    A label and a navigation toolbar are added to the form.

  5. Drag Orders from the Data Sources window to Form1. Place it under CompanyName.

  6. Right-click Form1 in Solution Explorer and then click View Code.

  7. Add the following code to the form, at the top of the Form class, outside any specific method.

    Private NorthwindSampleDataContext1 As New _
        NorthwindSampleDataContext
    
  8. Create an event handler for the Form_Load event by double-clicking the form. Add the following code to the event handler:

    CustomerBindingSource.DataSource = _
        NorthwindSampleDataContext1.Customers()
    
  9. Press F5 to run the code.

  10. When the form opens, click the Move next button on the toolbar to scroll through the companies and verify that the orders data is updated to display the selected customer's orders.

Saving Changes

When you add the controls to the O/R Designer, the Save Data button on the navigation toolbar is not enabled. If you want to enable users to save changes, you must first enable the save button and then add code to save changed data.

To enable the save button

  1. In Design view, select the save button on the navigation toolbar. (The button with the floppy disk icon.)

  2. In the Properties window, set the Enabled property to True.

  3. Double-click the save button to create an event handler and switch to the Code Editor.

  4. Add the following code into the save button event handler:

    NorthwindSampleDataContext1.SubmitChanges()
    
  5. Press F5 to run the code.

  6. Change the value in one of the Shipped Date fields and press the TAB key to commit the change.

  7. Click the Save Data button.

  8. Close the form.

  9. Press F5, and verify that the change you made is saved.

Next Steps

In this lesson you learned how to create LINQ to SQL classes by using the O/R Designer. In the next lesson, you will learn how to bind data by using a LINQ query. To learn more about how to use LINQ in an application, see Using LINQ in Visual Basic Express.

Next Lesson: Using LINQ to Bind Data to Controls

See Also

Tasks

Storing and Accessing Data

Displaying Related Data

Walkthrough: Creating LINQ to SQL Classes (O/R Designer)

Concepts

Using LINQ in Visual Basic Express

O/R Designer Overview

Other Resources

Managing Your Records: Using Data in Your Program