How to: Map a Void Method
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
Sometimes your stored procedure may not return values. This topic demonstrates how to handle this type of scenarios.
To complete this walkthrough, you will need to create a new domain model based on the SofiaCarRental database.
Mapping a Domain Method Result to No Value
Suppose, you have the following stored procedure (see the code-snippet below). It is based on the SofiaCarRental database. The procedure takes a single @RentalOrderId parameter and modifies the OrderStatus for the corresponding order.
CREATE PROCEDURE ChangeOrderStatus (@RentalOrderId INT)
AS
BEGIN
UPDATE RentalOrders
SET OrderStatus = 'completed'
WHERE RentalOrderID = @RentalOrderId
END
If you have already generated a domain model, you could include the ChangeOrderStatus stored procedure by using the Update From Database Wizard.
Follow the same steps as in the How to: Create a Domain Method for a Stored Procedure topic. Except this time, you need to select the None result type rather than a Persistent Type. The None result type indicates that the generated domain method does not return a value.
What Just Happened ?
When you click OK, the new ChangeOrderStatus method will be added to your context class. The signature of the generated method is:
public int ChangeOrderStatus(int? rentalOrderId)
{
}
Public Function ChangeOrderStatus(ByVal rentalOrderId As Integer?) As Integer
End Function
How It Works ?
The domain method internally uses the ExecuteNonQuery method exposed by the OpenAccessContext. For more information, please refer to How the Generated Methods Retrieve the Data.
public int ChangeOrderStatus(int? rentalOrderId)
{
OAParameter parameterRentalOrderId = new OAParameter();
parameterRentalOrderId.ParameterName = "RentalOrderId";
parameterRentalOrderId.Value = rentalOrderId;
int queryResult = this.ExecuteNonQuery("ChangeOrderStatus", CommandType.StoredProcedure,
parameterRentalOrderId);
return queryResult;
}
Public Function ChangeOrderStatus(ByVal rentalOrderId As Integer?) As Integer
Dim parameterRentalOrderId As New Telerik.OpenAccess.Data.Common.OAParameter
parameterRentalOrderId.ParameterName = "RentalOrderId"
parameterRentalOrderId.Value = rentalOrderId
Dim queryResult As Integer = Me.ExecuteNonQuery("ChangeOrderStatus",
CommandType.StoredProcedure, parameterRentalOrderId)
Return queryResult
End Function
The generated context method allows you to call the corresponding stored procedure from your code. Note that you need to invoke SaveChanges to commit the changes to the database. The point here is that when you create stored procedures in the database and map them to domain methods in the context, Telerik Data Access cannot verify what exactly these methods do. So, once you call them in your code, the changes they do in the database are not automatically committed (they are rolled back when the context is disposed). In order to persist the changes done by the stored procedures, you have to call the SaveChanges method of the context after the domain method is executed. For example:
using ( EntitiesModel dbContext = new EntitiesModel() )
{
dbContext.ChangeOrderStatus( 1 );
dbContext.SaveChanges();
}
Using dbContext As New EntitiesModel()
dbContext.ChangeOrderStatus(1)
dbContext.SaveChanges()
End Using