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 or Database Explorer onto the LINQ to SQL tools in Visual Studio. 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 or 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 run-time 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 this article. You might be using a different edition of Visual Studio or different environment settings. For more information, see Personalize the IDE.
To create LINQ to SQL classes that are mapped to database tables or views
In Server or Database Explorer, expand Tables or Views and locate the database table or view that you want to use in your application.
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
On the Build menu, click Build Solution to build your project.
To open the Data Sources window, on the Data menu, click Show Data Sources.
In the Data Sources window, click Add New Data Source.
Click Object on the Choose a Data Source Type page, and then select Next.
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.
Click Finish to create the data source and add the Customer entity class to the Data Sources window.
Drag items from the Data Sources window onto a form.
Related content
- LINQ to SQL tools in Visual Studio
- Walkthrough: Creating LINQ to SQL classes (O-R Designer)
- DataContext methods (O/R Designer)
- How to: Create DataContext methods mapped to stored procedures and functions (O/R Designer)
- LINQ to SQL object model
- Walkthrough: Customizing the insert, update, and delete behavior of entity classes
- How to: Create an association (relationship) between LINQ to SQL classes (O/R Designer)