This documentation is archived and is not being maintained.

ObjectContext.ExecuteFunction<TElement> Method

Executes the given stored procedure or function against the data source to return an ObjectResult<T>.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

protected ObjectResult<TElement> ExecuteFunction<TElement>(
	string functionName,
	params ObjectParameter[] parameters
)
where TElement : IEntityWithChangeTracker

Type Parameters

TElement

The entity type of the ObjectResult<T> returned when the function is executed against the data source. This type must implement IEntityWithChangeTracker.

Parameters

functionName
Type: System.String

Name of stored procedure or function. The name may include the container name, such as <Container Name>.<Function Name>. When the default container name is known, only the function name is required.

parameters
Type: System.Data.Objects.ObjectParameter[]

An array of ObjectParameter objects.

Return Value

Type: System.Data.Objects.ObjectResult<TElement>
An ObjectResult<T> for the data that is returned by the stored procedure.

ExceptionCondition
InvalidOperationException

The entity reader does not support this function.

-or-

There is a type mismatch on the reader and the function.

ArgumentException

function is null or empty

-or-

function is not found.

The ExecuteFunction<TElement> method is a helper method that is used to execute stored procedures or functions that are defined in the data source and expressed in the Entity Data Model (EDM). Entity Data Model tools generate a method for each FunctionImport Element in the conceptual model. These methods call a strongly-typed ExecuteFunction to return a typed ObjectResult<T>. For more information, see Application Code using Stored Procedures (Enity Framework).

All parameters for the function are required in the parameters array and all types are checked against the metadata for the import function, including the type of the function itself. null values are permitted for CLR value types. Parameter validation is performed by the provider.

The following example shows how to bind the ObjectResult<T> returned by the GetStudentGrades function. For the complete example, see the Walkthrough: Retrieving Entity Types with a Stored Procedure topic in the Entity Data Model tools documentation.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;
using System.Data.Objects.DataClasses;

namespace CourseManager
{
    public partial class GradeViewer : Form
    {
        // Create an ObjectContext instance based on SchoolEntity. 
        private SchoolEntities schoolContext;

        public GradeViewer()
        {
            InitializeComponent();
        }

        private void GradeViewer_Load(object sender, EventArgs e)
        {
            schoolContext = new SchoolEntities();

            // Define the query to retrieve students.
            ObjectQuery<Person> studentQuery = schoolContext.Person
                .Where("it.EnrollmentDate is not null")
                .OrderBy("it.LastName");

            // Execute and bind the studentList control to the query.
            studentList.DataSource = studentQuery
                .Execute(MergeOption.OverwriteChanges);
            studentList.DisplayMember = "LastName";
        }

        private void studentList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the selected student so we can use the 
            // PersonID in the function import call.
            Person currentStudent = (Person)this.studentList
                .SelectedItem;

            // Set the data source for the gradeGridView 
            // to the results returned by the GetStudentGrades 
            // Function Import.
            gradeGridView.DataSource = schoolContext
                .GetStudentGrades(currentStudent.PersonID);
        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1
Show: