简体   繁体   中英

Entity Framework calling stored procedure and not getting result

I have called a stored procedure with EF code-first DbContext before, but I've tried several ways of doing it tonight and I am not getting any progress.

The stored procedure returns a 0 or 1

DECLARE @return_value int

EXEC @return_value = [dbo].[CustomerKeyChecker]
                          @ldccode = 'nstar'   --SET @listofCodes = 'CLP, UIC, NSTAR, NSTARB, NSTARC, PSNH'

SELECT  'Return Value' = @return_value   

In my stored procedure, I have 1 incoming varchar(12) parameter, and then I do this:

SELECT @result = sign(charindex(' ' + @ldccode + ',', ' ' + @listofCodes + ','))
RETURN @result

So from executing it, that runs fine ...

Now from my C# Entity Framework code first, I either get Null or count of zero or that is will return many results...

poco

public class CustomerKey
{
    public int Key { get; set; }
}

1st attempt:

var param = new SqlParameter("@ldccode", key);
var result = _db.Database.SqlQuery<CustomerKey>("dbo.CustomerKeyChecker @ldccode", param).FirstOrDefault();

That returns null.

Next I try this

string sproc = "dbo.CustomerKeyChecker @ldccode=" + key;
var context = (_db as IObjectContextAdapter).ObjectContext;
var result2 = context.ExecuteStoreQuery<CustomerKey>("exec " + sproc);

I keep trying to use <string> , <int> , List<...> , I would THINK that it should pass back the result of a single value of int or string ... I do not want to use EDMX, and I need to use this stored procedure. And I really don't want to resort to using old school ado.net command etc..

Is it that my stored procedure is not doing a select * from ... instead it calls return ?

Or it is that my code is not mapped to poco and table entity?

Is it that my stored procedure is not doing a select * from ... instead it calls >return?

Yes.

My assumption - Jeremy started with EF code first and is open to a EF (database first) approach.

REASON - The template builder for EF (including v6) incorrectly sets the SP up as returning an INT containing the row count rather than the return value because it incorrectly calls the wrong ObjectContext.ExecuteFunction (found in the template-generated class YourDatabaseEntities that is the child of the DBContext).

Why wrong ExecuteFunction? - The result set incorrectly says the row count of changed rows rather than the return value or output parameters because it calls a different ExecuteFunction that discards the results. The flyover intellisense hint of the ObjectContext.ExecuteFunction says "Executes a stored procedure ….; discards any results returned from the function; and returns the number of rows affected by the execution " rather than the usual "Executes a stored procedure …. with the specified parameters ".

WHY -1 OR NULL : I believe the SET NOCOUNT ON is causing the SP to return no count result and that Microsoft's ExecuteFunction returns that as error code.

SP FIX : 1) You have to comment out SET NOCOUNT ON if you have one. 2) You have to change SP to do the SELECT command as last statement instead of the RETURN command.

CODE FIX :

var spResults = context.LTM_Lease_DeleteSubFiles(...);  -- ... is your parameters
int? spReturnValueResult = spResults.FirstOrDefault();  -- first one is return value.

SOLUTION FIX - 1) After fixing SP, delete SP from Function Imports folder and the Data Store's SP folder. 2) Reload the SP into the EDMX by using the "Update Model from Database" 3) Rebuild all of your data project where the EDMX resides. 4) Exit Visual Studio and return. 5) Rebuild overall solution.

See: Entity Framework (Database first) has incorrect return result from stored procedure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM