6 The DB2 Entity Framework Data Provider : Using Stored Procedures with the ADO.NET Entity Framework

Using Stored Procedures with the ADO.NET Entity Framework
NOTES:
Refer to the DataDirect Connect Series for ADO.NET Reference for more information about using stored procedures.
Using Multiple Result Sets
The DB2 Entity Framework data provider supports multiple result sets and return codes from DB2 stored procedures when parameters are bound as return codes. The stored procedures must take an argument list.
Using Pseudo Stored Procedures
The Connection object includes properties and methods that provide reauthentication and enhanced statistics functionality. The methods and properties are standard in the ADO.NET data provider, but are not available at the ADO.NET Entity Framework layer. Instead, you expose the same functionality through "pseudo" stored procedures.
This approach uses the Entity Data Model (EDM) to achieve results that correspond to the ADO.NET results. This in effect provides entities and functions backed by pseudo stored procedures.
Table 6-6 lists the mapping of the DB2Connection properties to the corresponding pseudo stored procedure.
 
You can create a function mapping in the entity model to invoke the pseudo-stored procedure. Alternatively, applications can use the ObjectContext to create a stored procedure command as shown in the following C# code fragment:
using (MyContext context = new MyContext())
{
   EntityConnection entityConnection = (EntityConnection)context.Connection;
   // The EntityConnection exposes the underlying store connection
   DbConnection storeConnection = entityConnection.StoreConnection;
   DbCommand command = storeConnection.CreateCommand();
   command.CommandText = "DDTek_Connection_EnableStatistics";
   command.CommandType = CommandType.StoredProcedure;
   command.Parameters.Add(new DB2Parameter("cid", 1));
}
 
bool openingConnection = command.Connection.State == ConnectionState.Closed;
if (openingConnection) { command.Connection.Open(); }
int result;
try
{
   result = command.ExecuteNonQuery();
}
finally
{
   if (openingConnection && command.Connection.State == ConnectionState.Open)
   {
      command.Connection.Close();
   }
}