Compartir a través de


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

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 Customizing Development Settings.

To create DataContext methods that return automatically generated types

  1. In Server Explorer/Database Explorer, expand the Stored Procedures node of the database you are working with.

  2. 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

  1. In Server Explorer/Database Explorer, expand the Stored Procedures node of the database you are working with.

  2. 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

Tasks

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

How to: Write LINQ Queries in C#

Concepts

DataContext Methods (O/R Designer)

Introduction to LINQ in Visual Basic

Other Resources

Object Relational Designer (O/R Designer)

LINQ to SQL [LINQ to SQL]