Muokkaa

Jaa


DataContext Methods (O/R Designer)

DataContext methods (in the context of the LINQ to SQL Tools in Visual Studio) 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 runs the stored procedure or function in the database to which the DataContext method is mapped. 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.

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 created by using the O/R Designer. By default, the Methods pane is empty; drag stored procedures or functions from Server Explorer or 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 or 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 has the name that matches the stored procedure or function name and properties, which 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 are named automatically, based on the name of the objects in the database. If you drag the same object more than once, a number is added 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.