0

I want to get @Return_Value from stored procedure using entity.

Lets say stored procedure is:

CREATE PROCEDURE GetEmployes( @whereSql nvarchar(512) )
AS
BEGIN
    set @tsql = 'select * from Employes where '  + @whereSql
    exec(@tsql)
    return 5
END

Entity imports is as

ObjectResult<Employe> GetEmployes(...)

This is what I need.

int return_value;
var result = db.GetEmpleys("name = .. AND ...", out return_value);
if (return_value == 5)
     // do something on this exit code
List<Employe> result.ToList<Employe>() ;

I am using Visual Studio 2008

TIA

1 Answer 1

1

The notion of the int return value is SQL Server specific. I don't believe that the EF, which is designed to be DB-server-agnostic, supports it.

So you have to use proper output params, not the int return value. That should not be a problem. Worst case, wrap the proc in another proc which returns the int return value of the first as an output of the second.

Finally, I hope this is just an example, as your demo proc introduces an SQL injection vulnerability.

3
  • Did that. I am wondering about alternatives. About sql injection, this is just a sample. Also, your proposal is not without its own headaches. I added the procedure myself using partial entity class with DbCommand. The problems are - entity doesn't support return value at all, it seems, DbCommand throws an error if RETURN is the last statement of stored proc (it must be Select). Then, the code in partial class can influence the other entity code. All in all, the question was not about workarounds. After reading around I guess flexibility and features of 1st version of Entity causes the issue.
    – majkinetor
    Mar 9, 2010 at 16:14
  • Even EF 4 doesn't support return value AFAIK, although it does support complex types and has much better support for scalar output. Mar 9, 2010 at 16:28
  • Scalar output is one way to go. Since RETURN statement can be problematic I use currently flag which determines what type of return procedure will output for integers - Select or Return.
    – majkinetor
    Mar 9, 2010 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.