简体   繁体   English

调用返回整数的过程时,数据读取器有多个字段错误

[英]The data reader has more than one field error while calling a procedure that returns an integer

I was trying to get status code of a stored procedure execution when encountered this error: 遇到此错误时,我试图获取存储过程执行的状态代码:

An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code EntityFramework.SqlServer.dll中出现“System.Data.Entity.Core.EntityCommandExecutionException”类型的异常,但未在用户代码中处理

Additional information: The data reader has more than one field. 附加信息:数据读取器具有多个字段。 Multiple fields are not valid for EDM primitive or enumeration types. 多个字段对EDM原语或枚举类型无效。

I have rewritten the procedure to do absolutely nothing but to return an integer value, this is how it looks now: 我已经重写了程序,除了返回一个整数值之外什么都不做,这就是它现在的样子:

ALTER Procedure [dbo].[test]
(
    @i int,
    @a nvarchar(max),
    @b nvarchar(max),
    @c nvarchar(max),
    @d nvarchar(max)
)
As
Begin
    SET NOCOUNT ON

    return 1
End

But I still get the same error at run-time when reaching the procedure call at line: 但是在到达行的过程调用时,我仍然在运行时得到相同的错误:

this.Database.SqlQuery<int>("test @i, @a, @b, @c, @d", p_i, p_a, p_b, p_c, p_d).FirstOrDefault();

Is there anyway to figure out what these fields are, and where they are coming from? 反正有没有弄清楚这些领域是什么,它们来自哪里? And how should I get the returned value? 我该如何获得返回值?

I've tried to specify a tuple of two strings as the T just to look into these values, but with no success... 我试图将两个字符串的元组指定为T只是为了查看这些值,但没有成功......

Updates: 更新:
Select 1 instead of return 1 makes the function usable, the only question remains what are these mysterious fields that are returned to the data reader? Select 1而不是return 1使函数可用,唯一的问题仍然是这些返回数据读取器的神秘字段是什么?

Database.SqlQuery<T>() expects some kind of result set (eg SELECT ). Database.SqlQuery<T>()需要某种结果集(例如SELECT )。 Under the hood, it uses DbCommand.ExecuteReader() , and when T is scalar, it expects the result set to have exactly one field -- but if the result set has more than one field, or if there are no fields , it throws the exception that you encountered. 在引擎盖下,它使用DbCommand.ExecuteReader() ,当T是标量时,它期望结果集只有一个字段 - 但如果结果集有多个字段, 或者如果没有字段 ,则抛出你遇到的例外。

The return value can be retrieved by passing a DbParameter to Database.SqlQuery<T>() and setting Direction = ParameterDirection.ReturnValue as seen in these examples: 可以通过将DbParameter传递给Database.SqlQuery<T>()并设置Direction = ParameterDirection.ReturnValue来检索返回值,如以下示例所示:


FYI, if you just want the return value, but you don't want a result set, use ExecuteSqlCommand with a DbParameter . 仅供参考,如果您只想要返回值,但不想要结果集,请使用带有DbParameter ExecuteSqlCommand

试试这个

var result = this.Database.SqlQuery<int>("exec test {0}, {1}, {2}, {3}, {4}", p_i, p_a, p_b, p_c, p_d).FirstOrDefault();

I think the answer given by Charlie is right. 我认为查理给出的答案是正确的。 But you have to write the code as follows. 但是你必须按如下方式编写代码。

var result = this.Database.SqlQuery<int>(string.Format("exec test {0}, {1}, {2}, {3}, {4}", p_i, p_a, p_b, p_c, p_d)).FirstOrDefault();

I came to a similar situation, I was calling a procedure from EF and the procedure had some print statements, EF was getting confused as it reads all the prints as an output. 我遇到了类似的情况,我从EF调用一个程序,并且程序有一些打印语句,EF因为将所有打印作为输出读取而感到困惑。 Make sure you comment all the prints before using it in EF. 确保在EF中使用之前评论所有打印件。

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

相关问题 实体框架中的“数据读取器有多个字段”错误 - “The data reader has more than one field” error in Entity Framework 数据读取器具有多个字段。 多个字段对于存储过程中的EDM原语或枚举类型无效 - The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types from a stored procedure 数据读取器具有多个字段。 多个字段对于EDM原语或枚举类型无效 - The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types 子文档数组中的项目单个字段返回多个 - Project single field in array of subdocument returns more than one 调用存储过程时“序列包含多个匹配元素” - “Sequence contains more than one matching element” when calling Stored Procedure 有多个入口点定义错误 - Has more than one entry point defined error 错误:程序有多个入口点 - Error: Program has more than one entry point 多个字段的共享验证错误消息 - Shared validation error message for more than one field 如何在 C# 中执行具有多个外参数的存储过程 - How to exec stored Procedure which has more than one outparameter in C# C# Interop.Excel 中显示的错误:源数据中的字段具有比数据透视表报告更多的唯一项 - Error showing in C# Interop.Excel: A field in your source data has more unique items than can be used in a PivotTable report
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM