Condividi tramite


Procedura: eseguire una query utilizzando una stored procedure con parametri In e Out (Entity Framework)

In questo argomento vengono forniti due esempi di come eseguire una stored procedure con parametri utilizzando Entity Framework . Nel primo esempio viene preso un parametro di input e viene restituita una raccolta di oggetti entità. Nel secondo esempio vengono presi un parametro di input e uno di output e viene restituito un valore nel parametro di output. Gli esempi inclusi in questo argomento sono basati sul modello School. Per seguire questi esempi, aggiungere il Modello School al progetto e configurarlo per l'utilizzo di Entity Framework . Per ulteriori informazioni, vedere Procedura: utilizzare la procedura guidata Entity Data Model (Entity Framework).

Esempio

Nel codice seguente viene eseguita una stored procedure GetStudentGrades, dove StudentId è un parametro obbligatorio. Per eseguire questo esempio di codice, importare la stored procedure GetStudentGrades e specificare entità CourseGrade come tipo restituito. Per informazioni sull'importazione di una stored procedure, vedere How to: Import Stored Procedures.

' Specify the Student ID. 
Dim studentId As Integer = 2

Using context As New SchoolEntities()
    For Each grade As StudentGrade In context.GetStudentGrades(studentId)
        Console.WriteLine("StudentID: " & studentId)
        Console.WriteLine("Student grade: " & grade.Grade)


    Next
End Using
// Specify the Student ID.
int studentId = 2;

using (SchoolEntities context =
    new SchoolEntities())
{
    foreach (StudentGrade grade in
              context.GetStudentGrades(studentId))
    {
        Console.WriteLine("StudentID: " + studentId);
        Console.WriteLine("Student grade: " + grade.Grade);
    }
}

Nel codice seguente viene eseguita la stored procedure GetDepartmentName che restituisce il nome del dipartimento nel parametro di output. Per eseguire questo esempio di codice:

  1. aggiungere la procedura seguente al database School:

    CREATE PROCEDURE [dbo].[GetDepartmentName]
          @ID int,
          @Name nvarchar(50) OUTPUT
          AS
          SELECT @Name = Name FROM Department
          WHERE DepartmentID = @ID
    
  2. Aggiornare il modello School dal database mediante l'aggiunta di stored procedure GetDepartmentName. Per ulteriori informazioni, vedere How to: Update an .edmx File when the Database Changes.

  3. Importare la stored procedure GetDepartmentName e specificare None come tipo restituito di questa stored procedure. Per ulteriori informazioni, vedere How to: Import a Stored Procedure.

Using context As New SchoolEntities()
    ' name is an output parameter. 
    Dim name As New ObjectParameter("Name", GetType(String))
    context.GetDepartmentName(1, name)

    Console.WriteLine(name.Value)
End Using
using (SchoolEntities context =
    new SchoolEntities())
{
    // name is an output parameter.
    ObjectParameter name = new ObjectParameter("Name", typeof(String));
    context.GetDepartmentName(1, name);
    Console.WriteLine(name.Value);

}