How to: Create DataContext methods mapped to stored procedures and functions (O/R Designer)
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. 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
You can add stored procedures and functions 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
You can also use stored procedures to override the default LINQ to SQL run-time 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).
Create DataContext methods
You can create DataContext methods by dragging stored procedures or functions from Server Explorer or **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 this article. You may be using a different edition of Visual Studio or different environment settings. For more information, see Personalize the IDE.
To create DataContext methods that return automatically generated types
In Server Explorer or Database Explorer, expand the Stored Procedures node of the database with which you are working.
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 or Database Explorer, expand the Stored Procedures node of the database with which you are working.
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).