Creating LINQ to SQL Classes with the O/R Designer

Visual Studio 2008

You can use the Object Relational Designer (O/R Designer) to map 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 C#.

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

  • You must also have access to the SQL Server version of the Northwind sample database. For information about how to obtain and install this database, see How to: Install Sample Databases.

To open the O/R Designer

  1. On the File menu, click New Project.

  2. In the New Project dialog box, click Windows Forms Application, name it 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 Classes file (NorthwindSample.dbml) is added to the project, and the O/R Designer opens.

The O/R Designer consists 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 defined in the file. The methods pane is the design surface that displays the DataContext methods 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 its name is NorthwindSampleDataContext in the Properties window. You can create entity classes by dragging database tables from Database Explorer onto the O/R Designer surface. But first, you must create a connection to the database. This process resembles what was described in How to: Connect to a Database (C#), except that here you will use a SQL Server Express database.

To connect to the Northwind sample database

  1. On the View menu, point to Other Windows, and then click Database Explorer.

    Database Explorer opens.

  2. Click the Connect to Database button.

    The Add Connection dialog box opens.

  3. In the Add Connection dialog box, make sure that the Data source is Microsoft SQL Server Database File.

  4. Click Browse and select the Northwind.mdf database.

  5. Click Test Connection to verify that the connection was successful.

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

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 were selected.

To create entity classes

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

  2. Drag Customers from Database Explorer onto the O/R Designer surface. If 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.

    NoteNote:

    The entity class name is Customer, instead of Customers. This renaming behavior is called pluralization. It can be turned on or off in the Options dialog box. For more information, see Options Dialog Box (Visual Studio) and How to: Turn Pluralization On and Off (O/R Designer).

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

    An entity class named Order is created. Notice that the Order class has a relationship (known as an association) with the Customer entity class. The properties of the Order class 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 Solution.

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

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

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

  5. Expand LINQToSQLSample and click the Customer class.

    NoteNote:

    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.

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 next to CompanyName and click Label.

  4. Drag CompanyName 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 NorthwindSampleDataContext northwindSampleDataContext1
        = new NorthwindSampleDataContext();
    
  8. Create an event handler for the Form_Load event and 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 bindingNavigatorMoveNextItem button to scroll through the companies and verify that the ordersDataGridView is updated to display the selected customer's orders.

When you add controls to the O/R Designer, the Save button on the bindingNavigator 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. Click the Save button on the bindingNavigator. (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 button.

  8. Close the form.

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

Community Additions

ADD
Show: