简体   繁体   English

实体框架调用存储过程但未获得结果

[英]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. 之前我已经使用EF代码优先的DbContext调用了存储过程,但是今晚我尝试了几种方法来执行存储过程,但是没有任何进展。

The stored procedure returns a 0 or 1 存储过程返回0或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: 在我的存储过程中,我有1个传入的varchar(12)参数,然后执行此操作:

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... 现在首先从我的C#实体框架代码中获取Null或计数为零,否则将返回许多结果...

poco 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. 返回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. 我一直尝试使用<string><int>List<...> ,我想它应该传回intstring的单个值的结果...我不想使用EDMX,并且我需要使用此存储过程。 And I really don't want to resort to using old school ado.net command etc.. 而且我真的不想诉诸于使用老派的ado.net命令等。

Is it that my stored procedure is not doing a select * from ... instead it calls return ? 是不是我的存储过程没有执行select * from ...而是调用return

Or it is that my code is not mapped to poco and table entity? 还是我的代码未映射到poco和表实体?

Is it that my stored procedure is not doing a select * from ... instead it calls >return? 是不是我的存储过程没有执行select * from ...而是调用了> return?

Yes. 是。

My assumption - Jeremy started with EF code first and is open to a EF (database first) approach. 我的假设-杰里米(Jeremy)首先开始使用EF代码,并且对EF(数据库优先)方法持开放态度。

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). 原因-EF (包括v6)的模板构建器错误地将SP设置为返回包含行数而不是返回值的INT,因为它错误地调用了错误的ObjectContext.ExecuteFunction(在模板生成的类YourDatabaseEntities中, 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. -结果集错误地表示已更改行的行数,而不是返回值或输出参数,因为它调用了另一个ExecuteFunction来丢弃结果。 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 ". 该ObjectContext.ExecuteFunction的天桥智能感知提示说,“执行存储过程...; 丢弃该函数返回任何结果;以及返回受执行行数 ”,而不是通常的“执行存储过程...... 指定参数 ”。

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. 为什么为-1或NULL :我相信SET NOCOUNT ON导致SP不返回任何计数结果,并且Microsoft的ExecuteFunction将其作为错误代码返回。

SP FIX : 1) You have to comment out SET NOCOUNT ON if you have one. SP FIX :1)如果有一个,则必须注释掉SET NOCOUNT ON。 2) You have to change SP to do the SELECT command as last statement instead of the RETURN command. 2)您必须更改SP以将SELECT命令作为最后一条语句而不是RETURN命令来执行。

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. 解决方案 -1)修复SP后,从Function Imports文件夹和Data Store的SP文件夹中删除SP。 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. 2)通过使用“从数据库更新模型”将SP重新加载到EDMX中。3)重建EDMX所在的所有数据项目。 4) Exit Visual Studio and return. 4)退出Visual Studio并返回。 5) Rebuild overall solution. 5)重建整体解决方案。

See: Entity Framework (Database first) has incorrect return result from stored procedure 请参阅: 实体框架(数据库优先)从存储过程中返回了错误的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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