Stored Procedures
The following example shows how to call a stored procedure using Entity Framework. Define a class to capture the results of the stored procedure; for example, the Name class shown below. This class should define a property for each output column that you are interested in using. Once this class is defined, you can use the ExecuteStoreQuery method of the Entity Framework context to execute a stored procedure using the EXEC SQL command.
C#
public class GetRolesOutput {
public string Name { get; set; }
// ... add other return values with matching data type
}
public class Program {
static void Main(string[] args) {
using (NetSuiteEntities context = new NetSuiteEntities())
{
//use the line below for versions earlier than EF 5:
//IEnumerable<GetRolesOutput> result = context.ExecuteStoreQuery<GetRolesOutput>("EXEC GetRoles @Account_Name = ?;",new NetSuiteParameter("Account_Name", "Checking"));
//use the line below for EF 5 and 6:
IEnumerable<GetRolesOutput> result = context.Database.SqlQuery<GetRolesOutput>("EXEC GetRoles=@Account_Name", new NetSuiteParameter("Account_Name", "Checking"));
List<GetRolesOutput> resultList = result.ToList<GetRolesOutput>();
foreach (GetRolesOutput value in resultList) {
Console.WriteLine("Name: " + value.Name);
}
}
}
}
VB.NET
Public Class GetRolesOutput
Public Property Name() As String
' ... add other return values with matching data type
End Class
Module Program
Sub Main
Using context As New NetSuiteEntities
'use the line below for versions earlier than EF 5:
'Dim result As IEnumerable(Of GetRolesOutput) = context.ExecuteStoreQuery(Of GetRolesOutput)("EXEC GetRoles @Account_Name = ?;", New NetSuiteParameter("Account_Name", "Checking"))
'use the line below for EF 5 and 6:
Dim result As IEnumberable(Of GetRolesOutput) = context.Database.SqlQuery(Of GetRolesOutput)("EXEC GetRoles @Account_Name = ?;", New NetSuiteParameter("Account_Name", "Checking"))
Dim resultList As List(Of GetRolesOutput) = result.ToList()
For Each value As GetRolesOutput In resultList
Console.WriteLine("Name: " + value.Name)
Next
End Using
End Sub
End Module
You can avoid using NetSuiteParameter by passing in the parameters directly:
"EXEC GetRoles @Account_Name=Checking, ..."