How to: Create DataContext methods mapped to stored procedures and functions (O/R Designer)
Note
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
Stored procedures and functions can be added to the O/R Designer as DataContext methods. Calling the method and passing in the required parameters runs the stored procedure or function on the database and returns the data in the return type of the DataContext method. For detailed information about DataContext methods, see DataContext Methods (O/R Designer).
Note
Stored procedures can also be used to override the default LINQ to SQL runtime behavior that performs Inserts, Updates, and Deletes when changes are saved from entity classes to a database. For more information, see How to: Assign stored procedures to perform updates, inserts, and deletes (O/R Designer).
Creating DataContext Methods
You can create DataContext methods by dragging stored procedures or functions from Server Explorer/Database Explorer onto the O/R Designer.
Note
The return type of the generated DataContext method differs depending on where you drop the stored procedure or function on the O/R Designer. Dropping 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 creates a DataContext method that returns an automatically generated type. You can change the return type of a DataContext method after adding 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).
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 Personalizing the IDE.
To create DataContext methods that return automatically generated types
In Server Explorer/Database Explorer, expand the Stored Procedures node of the database you are working with.
Locate the desired stored procedure and drag it onto an empty area of the O/R Designer.
The DataContext method is created with an automatically generated return type and appears in the Methods pane.
To create DataContext methods that have the return type of an entity class
In Server Explorer/Database Explorer, expand the Stored Procedures node of the database you are working with.
Locate the desired stored procedure and drag it onto an existing entity class in the O/R Designer.
The DataContext method is created with the return type of the selected entity class and appears in the Methods pane.
Note
For information about changing the return type of existing DataContext methods, see How to: Change the return type of a DataContext method (O/R Designer).
See Also
LINQ to SQL Tools in Visual Studio DataContext Methods (O/R Designer) Walkthrough: Creating LINQ to SQL Classes (O-R Designer) LINQ to SQL Introduction to LINQ in Visual Basic How to: Write LINQ Queries in C#