i am new in EF and working with EF code first. just got a link https://code.msdn.microsoft.com/How-to-retrieve-output-e85526ba which show how to use read output type param by EF db first. so anyone tell me how to retrieve output parameter from stored procedure by EF code first ?
if possible give me small sample code or redirect me to relevant articles.
thanks
var outParam = new SqlParameter();
outParam.ParameterName = "TotalRows";
outParam.SqlDbType = SqlDbType.Int;
outParam.ParameterDirection = ParameterDirection.Output;
var data = dbContext.Database.SqlQuery<MyType>("sp_search @SearchTerm, @MaxRows, @TotalRows OUT",
new SqlParameter("SearchTerm", searchTerm),
new SqlParameter("MaxRows", maxRows),
outParam);
var result = data.ToList();
totalRows = (int)outParam.Value;
To use a Stored Procedure with the Code First model, we need to override the OnModelCreating method of DBContext and add the following code to map the Stored Procedure. The MapToStoreProcedures method has two overloaded methods, one method is without a parameter.
A Stored Procedure can have any number of output parameters. The simple logic is this — If you want to return 1 value then use 1 output parameter, for returning 5 values use 5 output parameters, for 10 use 10, and so on.
If you specify the output keyword for a parameter in the procedure definition, the procedure can return the current value of the parameter to the calling program when the procedure exits.
To create an output parameter for a stored procedure, you use the following syntax: parameter_name data_type OUTPUT. A stored procedure can have many output parameters. In addition, the output parameters can be in any valid data type e.g., integer, date, and varying character.
Code language:SQL (Structured Query Language)(sql) Calling stored procedures with output parameters To call a stored procedure with output parameters, you follow these steps: First, declare variablesto hold the values returned by the output parameters Second, use these variables in the stored procedure call.
Sometimes the stored procedures will return data that doesn't map to existing entities. It might return only a subset of the data required for an entity or it might return a scalar value.
Generating stored procedures. Entity Framework can be used to generate simple non-query procedures for entities if you prefer to use those rather than the DbSet's Add and Remove methods. You enable this feature as part of the migration configuration by using the MapToStoredProcedures method.
To retrieve the data for a stored procedure call, you can use the following
using(var db = new YourConext())
{
var details = db.Database.SqlQuery<YourType>("exec YourProc @p",
new SqlParameter("@p", YourValue));
}
YourType: might be int or string or long or even a ComplexType
@p: in case if the stored procedure has parameters and you can define as many as you need from parameters
if you need more information about SqlQuery , you might check the following
Hope this will help you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With