How to: Add LINQ to SQL Classes to a Project (O/R Designer)

Data classes (entity classes, associations, and DataContext methods) are defined in LINQ to SQL files (.dbml files) and created and edited in the Object Relational Designer (O/R Designer). You add LINQ to SQL files to projects by using the Add New Item dialog box and selecting the LINQ to SQL Classes template. For more information about data classes, see The LINQ to SQL Object Model.

Adding LINQ to SQL Data Classes to a Project

Create and edit LINQ to SQL classes by using the 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 Customizing Development Settings in Visual Studio.

To add a LINQ to SQL file to a project

  1. From within a Visual Basic or C# application, on the Project menu, click Add New Item.

  2. Click the LINQ to SQL Classes template.

  3. Either provide an alternative name or keep the default name of DataClasses1.dbml.

    Note

    The name provided will be the name of the generated DataContext. For example, using the default name will cause the designer to name the DataContext DataClasses1DataContext.

  4. Click Add.

    The .dbml file is added to the project and the O/R Designer opens.

After you add a LINQ to SQL file to your project, the empty design surface represents a DataContext ready to be configured. Drag database items from Server Explorer/Database Explorer onto the O/R Designer to create data classes and DataContext methods. The data connection of a DataContext is created based on the first item added to the designer form Server Explorer/Database Explorer.

The following topics provide detailed instructions for creating LINQ to SQL classes and DataContext methods by using the O/R Designer.

Topic

Description

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

Provides step-by-step directions for creating entity classes that map to tables and views in a database, including information about what is updateable and read only.

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

Provides step-by-step directions for creating DataContext methods that map to stored procedures and functions.

How to: Assign Stored Procedures to Perform Updates, Inserts, and Deletes (O/R Designer)

Describes how to create DataContext methods that replace the default save logic for an entity class with user-provided stored procedures.

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

Provides step-by-step instructions for designing entity classes by using the O/R Designer and for displaying data on a Windows Form.

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

Provides step-by-step directions for creating DataContext methods that replace the default save logic for an entity class with user-provided stored procedures.

Walkthrough: Adding Validation to Entity Classes

Provides step-by-step directions for adding code to validate data.

See Also

Tasks

Walkthrough: Adding Validation to Entity Classes

Concepts

O/R Designer Overview

DataContext Methods (O/R Designer)

DataContext Methods (O/R Designer)

Other Resources

LINQ to SQL [LINQ to SQL]

Query Concepts