Share via


How to: Create LINQ to SQL Classes Mapped to Tables and Views (O/R Designer)

LINQ to SQL classes that are mapped to database tables and views are called entity classes. The entity class maps to a record, whereas the individual properties of an entity class map to the individual columns that make up a record. Create entity classes that are based on database tables or views by dragging tables or views from Server Explorer/Database Explorer onto the Object Relational Designer (O/R Designer). The O/R Designer generates the classes and applies the specific LINQ to SQL attributes to enable LINQ to SQL functionality (the data communication and editing capabilities of the DataContext). For detailed information about LINQ to SQL classes, see The LINQ to SQL Object Model.

Note

The O/R Designer is a simple object relational mapper because it supports only 1:1 mapping relationships. In other words, an entity class can have only a 1:1 mapping relationship with a database table or view. Complex mapping, such as mapping an entity class to multiple tables, is not supported. However, you can map an entity class to a view that joins multiple related tables.

Create LINQ to SQL Classes That Are Mapped to Database Tables or Views

Dragging tables or views from Server Explorer/Database Explorer onto the O/R Designer creates entity classes in addition to the DataContext methods that are used for performing updates.

By default, the LINQ to SQL runtime creates logic to save changes from an updatable entity class back to the database. This logic is based on the schema of the table (the column definitions and primary key information). If you do not want this behavior, you can configure an entity class to use stored procedures to perform Inserts, Updates, and Deletes instead of using the default LINQ to SQL runtime behavior. For more information, see How to: Assign Stored Procedures to Perform Updates, Inserts, and Deletes (O/R Designer).

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To create LINQ to SQL classes that are mapped to database tables or views

  1. In Server/Database Explorer, expand Tables or Views and locate the database table or view that you want to use in your application.

  2. Drag the table or view onto the O/R Designer.

    An entity class is created and appears on the design surface. The entity class has properties that map to the columns in the selected table or view.

Create an Object Data Source and Display the Data on a Form

After you create entity classes by using the O/R Designer, you can create an object data source and populate the Data Sources Window with the entity classes.

To create an object data source based on LINQ to SQL entity classes

  1. On the Build menu, click Build Solution to build your project.

  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 nodes and locate and select your class.

    Note

    If the Customer class is not available, cancel out of the wizard, 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.

  7. Drag items from the Data Sources window onto a form.

See Also

Tasks

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

How to: Create DataContext Methods Mapped to Stored Procedures and Functions (O/R Designer)

How to: Add Validation to Entity Classes

Walkthrough: Customizing the Insert, Update, and Delete Behavior of Entity Classes

Walkthrough: Adding Validation to Entity Classes

How to: Create an Association (Relationship) Between LINQ to SQL Classes (O/R Designer)

Concepts

DataContext Methods (O/R Designer)

The LINQ to SQL Object Model

Other Resources

Object Relational Designer (O/R Designer)