Partilhar via


O/R Designer Overview

The Object Relational Designer (O/R Designer) provides a visual design surface for creating LINQ to SQL entity classes and associations (relationships) based on objects in a database. In other words, the O/R Designer is used to create an object model in an application that maps to objects in a database. It also generates a strongly-typed DataContext that is used to send and receive data between the entity classes and the database. The O/R Designer also provides functionality to map stored procedures and functions to DataContext methods for returning data and populating entity classes. Finally, the O/R Designer provides the ability to design inheritance relationships between entity classes. 

The O/R Designer generates the .dbml file that provides the mapping between the LINQ to SQL classes and database objects. The O/R Designer also generates the typed DataContext and the entity classes.

The O/R Designer has two distinct areas on its design surface: the entities pane on the left, and the methods pane on the right. The entities pane is the main design surface that displays the entity classes, associations, and inheritance hierarchies. The methods pane is the design surface that displays the DataContext methods that are mapped to stored procedures and functions.

Note

The O/R Designer currently supports only SQL Server 2000, SQL Server 2005, SQL Server 2008, and SQL Server Express databases. Although O/R Designer does not support SQL Server Compact 3.5, LINQ to SQL does. For more information, see SQL Server Compact 3.5 and LINQ to SQL. For information about how to obtain SQL Server Express, see the Obtaining SQL Server Express section in the How to: Install Sample Databases topic.

Opening the O/R Designer

You can open the O/R Designer by adding a new LINQ to SQL Classes item to a project. For detailed information, see How to: Add LINQ to SQL Classes to a Project (O/R Designer).

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 a joined table, is not currently supported. Additionally, the designer is a one-way code generator. This means that only changes that you make to the designer surface are reflected in the code file. Manual changes to the code file are not reflected in the O/R Designer. Any changes that you make manually in the code file are overwritten when the designer is saved and code is regenerated. For information about how to add user code and extend the classes generated by the O/R Designer, see How to: Extend Code Generated by the O/R Designer.

Creating and Configuring the DataContext

After you add a LINQ to SQL Classes item to a project and open the O/R Designer, the empty design surface represents an empty DataContext ready to be configured. the DataContext is configured with connection information provided by the first item that is dragged onto the design surface.. Therefore, the DataContext is configured by using connection information from the first item dropped onto the design surface. For more information about the DataContext class see, DataContext Methods (O/R Designer).

Creating Entity Classes That Map to Database Tables and Views

You can create entity classes mapped to tables and views by dragging database tables and views from Server Explorer/Database Explorer onto the O/R Designer. As indicated in the previous section the DataContext is configured with connection information provided by the first item that is dragged onto the design surface. If a subsequent item that uses a different connection is added to the O/R Designer, you can change the connection for the DataContext. For more information, see How to: Create LINQ to SQL Classes Mapped to Tables and Views (O/R Designer).

Creating DataContext Methods That Call Stored Procedures and Functions

You can create DataContext methods that call (are mapped to) stored procedures and functions by dragging them from Server Explorer/Database Explorer onto the O/R Designer. Stored procedures and functions are added to the O/R Designer as methods of the DataContext.

Note

When you drag stored procedures and functions from Server Explorer/Database Explorer onto the O/R Designer, the return type of the generated DataContext method differs depending on where you drop the item. For more information, see DataContext Methods (O/R Designer).

Configuring a DataContext to Use Stored Procedures to Save Data Between Entity Classes and a Database

As stated earlier, you can create DataContext methods that call stored procedures and functions. Additionally, you can also assign stored procedures that can be used for the default LINQ to SQL runtime behavior that performs Inserts, Updates, and Deletes. For more information, see How to: Assign Stored Procedures to Perform Updates, Inserts, and Deletes (O/R Designer).

Inheritance and the O/R Designer

Like other objects, LINQ to SQL classes can use inheritance and be derived from other classes. In a database, inheritance relationships are created in several ways. The O/R Designer supports the concept of single-table inheritance as it is often implemented in relational systems. For more information, see How to: Configure Inheritance by Using the O/R Designer.

LINQ to SQL Queries

The entity classes created by the O/R Designer are designed for use with Language-Integrated Query (LINQ). For more information, see How to: Query for Information (LINQ to SQL).

Separating the Generated DataContext and Entity Class Code into Different Namespaces

The O/R Designer provides the Context Namespace and Entity Namespace properties on the DataContext. These properties determine what namespace the DataContext and entity class code is generated into. By default, these properties are empty and the DataContext and entity classes are generated into the application's namespace. To generate the code into a namespace other than the application's namespace, enter a value into the Context Namespace and/or Entity Namespace properties.

See Also

Tasks

Walkthrough: Creating LINQ to SQL Classes (O/R Designer)
How to: Assign Stored Procedures to Perform Updates, Inserts, and Deletes (O/R Designer)

Other Resources

LINQ to SQL
LINQ General Programming Guide
LINQ to ADO.NET
What's New in Data
Object Relational Designer (O/R Designer)