Compartilhar via


DataContext Methods (O/R Designer)

DataContext methods (in the context of the Object Relational Designer (O/R Designer)) are methods of the DataContext class that run stored procedures and functions in a database.

The DataContext class is a LINQ to SQL class that acts as a conduit between a SQL Server database and the LINQ to SQL entity classes mapped to that database. The DataContext class contains the connection string information and the methods for connecting to a database and manipulating the data in the database. By default, the DataContext class contains several methods that you can call, such as the SubmitChanges method that sends updated data from LINQ to SQL classes to the database. You can also create additional DataContext methods that map to stored procedures and functions. In other words, calling these custom methods will run the stored procedure or function in the database that the DataContext method is mapped to. You can add new methods to the DataContext class just as you would add methods to extend any class. However, in discussions about DataContext methods in the context of the O/R Designer, it is the DataContext methods that map to stored procedures and functions that are being discussed.

Note

In LINQ to SQL, stored procedures and functions are handled in the same way, in that both stored procedures and functions map to entity classes by using the same StoredProcedureAttribute. In the context of LINQ to SQL, the DataContext methods that map to stored procedures are the same as those that map to functions.

Methods Pane

DataContext methods that map to stored procedures and functions are displayed in the methods pane of the O/R Designer. The methods pane is the pane along the side of the Entities pane (the main design surface). The methods pane lists all DataContext methods that you have created by using the O/R Designer. By default, the methods pane is empty; drag stored procedures or functions from Server Explorer/Database Explorer onto the O/R Designer to create DataContext methods and populate the methods pane. For more information, see How to: Create DataContext Methods Mapped to Stored Procedures and Functions (O/R Designer).

Note

Open and close the methods pane by right-clicking the O/R Designer and then clicking Hide Methods Pane or Show Methods Pane, or use the keyboard shortcut CTRL+1.

Two Types of DataContext Methods

DataContext methods are those methods that map to stored procedures and functions in the database. You can create and add DataContext methods on the methods pane of the O/R Designer. There are two distinct types of DataContext methods; those that return one or more result sets, and those that do not:

Return Types of DataContext Methods

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. Dropping the items directly onto an existing entity class creates a DataContext method with the return type of the entity class; dropping items onto an empty area of the O/R Designer (in either pane) creates a DataContext method that returns an automatically generated type. The automatically generated type that is created has a name that matches the stored procedure or function name and properties that map to the fields returned by the stored procedure or function.

Note

You can change the return type of a DataContext method after you add it to the methods pane. To inspect or change the return type of a DataContext method, select it and inspect the Return Type property in the Properties window. For more information, see How to: Change the Return Type of a DataContext Method (O/R Designer).

Objects you drag from the database onto the O/R Designer surface will be named automatically, based on the name of the objects in the database. If you drag the same object more than once, a number is appended to the end of the new name that differentiates the names. When database object names contain spaces, or characters not-supported in Visual Basic or C#, the space or invalid character is replaced with an underscore.

See Also

Tasks

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

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

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

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

Other Resources

Object Relational Designer (O/R Designer)

LINQ to SQL [LINQ to SQL]

Stored Procedures [LINQ to SQL]