gpt4 book ai didi

c# - 使用 Entity Framework 获取存储过程输出参数会引发映射错误 : The data reader is incompatible with the specified

转载 作者:行者123 更新时间:2023-11-30 21:39:13 26 4
gpt4 key购买 nike

我正在处理一个遗留应用程序,我正在尝试调用一个返回整数的存储过程,但我遇到了映射错误。错误本身就是这个:

The data reader is incompatible with the specified IF_Insert_Incidents_Citizen'. A member of the type, 'intID', does not have a corresponding column in the data reader with the same name.

我尝试将名称更改为 _intId,因为我知道 Entity Framework 会将所有以 @variable 开头的名称转换为 _variable

奇怪的是,如果我使用 LinqPad 并调用存储过程,我会得到 @intID 的正确值

enter image description here

enter image description here

这是存储过程:

CREATE PROCEDURE [dbo].[IF_Insert_Incidents_Citizen]
@chvFolio varchar(50),
@chvOwner_Name varchar(100),
@chvOwner_Address varchar(100),
@dtsDate_Created smalldatetime,
@intUser_ID integer,
@chvDescription varchar(250),
@chvEmail varchar(50),
@intIncident_Type_ID integer,
@intIncident_Status integer,
@intID int Output
AS
SET nocount on

DECLARE @intErrorCode INT
DECLARE @intApp_ID INT

SELECT @intIncident_Type_ID = CAST(Param_Value AS INT)
FROM IF_Parameters
WHERE Param_Name = 'Citizen_Incident_Type_ID'

BEGIN
--Get an application id
INSERT INTO MasterApplication (DateCreated)
VALUES (getdate())

SELECT @intApp_ID = SCOPE_IDENTITY()

INSERT INTO IF_Incidents
(
Folio,
Owner_Name,
Owner_Address,
Date_Created,
User_ID,
Description,
Incident_Type_ID,
Incident_Status,
App_ID
)
VALUES
(
@chvFolio,
@chvOwner_Name,
@chvOwner_Address,
@dtsDate_Created,
@intUser_ID,
@chvDescription ,
@intIncident_Type_ID,
@intIncident_Status,
@intApp_ID
)
Select @intErrorCode = @@Error, @intID = SCOPE_IDENTITY()

--Insert Complaint
INSERT INTO IF_Complaints
(
Incident_ID,
Complainant_ID,
Description ,
User_ID,
Officer_Assigned_ID,
Complaint_Status_ID,
Date_Made,
Email_Complainant
)
VALUES
(
@intID,
Null,
@chvDescription + ' at ' + @chvOwner_Address,
1,
1,
1,
@dtsDate_Created ,
@chvEmail
)
Select @intErrorCode = @@Error--, @intID = SCOPE_IDENTITY()
End

Return @intErrorCode

正如您在存储过程中看到的那样,intId 在插入后总是获得增量值,调用 SCOPE_IDENTITY()

现在我正尝试通过以下方式使用 Entity Framework 调用我的存储过程:

var bptRep = DependencyFactory.GetDependency<ICetRepository<IF_Insert_Incidents_Citizen>>();
var parameters = GetIfInsertIncidentsCitizenParamenters();
var res = bptRep.GetAllFromStoredProcedure(storedProcedure, parameters);

仓库使用这个类

//class with just the return value
public class IF_Insert_Incidents_Citizen
{
public int? intID { get; set; }
}

这里我获取了存储过程的所有参数

 private IEnumerable<SqlParameter> GetIfInsertIncidentsCitizenParamenters()
{
// Create Parameters
var parameters = new List<SqlParameter>();

var param1 = CreateSqlParameter("@chvFolio", SqlDbType.NVarChar, ParameterDirection.Input, criteria["chvFolio"].Value, 50);

parameters.Add(param1);

....

var paramX = CreateSqlParameter("@intIncident_Status", SqlDbType.Int, ParameterDirection.Input, 10);
parameters.Add(paramX);

var outparam = CreateSqlParameter("@intID", SqlDbType.Int, ParameterDirection.InputOutput, DBNull.Value);
parameters.Add(outparam);
}

public IQueryable<TEntity>GetAllFromStoredProcedure(string storedProcedure, IEnumerable<SqlParameter> parameters)
{
var result = Context.Database.SqlQuery<TEntity>(storedProcedure, parameters).AsQueryable();
return result;
}

我按照@GertArnold 建议的方法但仍然没有用,这是我的参数和代码

enter image description here

var returnCode = new SqlParameter("@ReturnCode", SqlDbType.Int);
returnCode.Direction = ParameterDirection.Output;

var sql = "exec IF_Insert_Incidents_Citizen @chvFolio, @chvOwner_Name, @chvOwner_Address, @dtsDate_Created, @intUser_ID, @chvDescription, @chvEmail, @intIncident_Type_ID, @intIncident_Status, @intID OUT";

var data = ctx.Database.SqlQuery<object>(sql, returnCode, parameters);

var result = data.FirstOrDefault();

最佳答案

最后@GertArnold 给我指明了正确的方向,如果你还没有读过这篇文章,请读一读,这会节省你很多时间 Stored procedures with output parameters using SqlQuery in the DbContext API

我只是在 exec 调用中将 OUT 添加到我的 @intId 中,无需创建 ReturnCode 变量并调用 exec @ReturnCode = ....

var returnCode = new SqlParameter("@ReturnCode", SqlDbType.Int);
returnCode.Direction = ParameterDirection.Output;

我的代码最终看起来像这样,请注意在参数上调用 .ToArray 方法,而不是传递 IEnumerableIList 的集合> 方法

var sql = "exec IF_Insert_Incidents_Citizen @chvFolio, @chvOwner_Name, @chvOwner_Address, @dtsDate_Created, @intUser_ID, @chvDescription, @chvEmail, @intIncident_Type_ID, @intIncident_Status, @intID OUT";

var data = ctx.Database.SqlQuery<object>(sql, returnCode, parameters.ToArray());

var result = data.FirstOrDefault();

关于c# - 使用 Entity Framework 获取存储过程输出参数会引发映射错误 : The data reader is incompatible with the specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45311855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com