来自 EF 映射对象的不兼容数据读取器异常 [英] Incompatible Data Reader Exception From EF Mapped Objects
问题描述
我正在使用实体框架并更新了一个表及其存储过程,但在调用存储过程时出现以下错误.
I am using Entity Framework and have updated a table and its stored procedure but I'm getting the following error when the stored procedure is called.
数据读取器与指定的不兼容'FormValueModel.Valuation'.'ValuationId' 类型的成员执行数据读取器中没有对应的同名列.
The data reader is incompatible with the specified 'FormValueModel.Valuation'. A member of the type, 'ValuationId', does not have a corresponding column in the data reader with the same name.
ValuationId 是我想要自动递增的主键.
ValuationId is my primary key witch i want to auto increment.
我可以执行从 SQL 管理工作室找到的存储过程,当我运行我的应用程序时,它会写入数据库,但随后出现错误消息.
I can execute the stored procedure find from SQL management studio, And when i run my application it writes into the database but then the error message appears.
我不熟悉实体框架,只有基础知识,我认为这可能是 model.edmx 的映射问题.
I'm unfamiliar with Entity Framework and just have the basics, and i think it may be a mapping issue from the model.edmx.
重新创建和映射模型中的表和存储过程的正确过程是什么?
What would be the correct procedure in recreating and mapping the tables and stored procedures in the model?
存储过程.
ALTER PROCEDURE [dbo].[ValuationCreate]
@TrackingNumber varchar(100),
@FormMobiValuationId varchar(100),
@ValuationPropertyId int,
@ValuationFileName varchar(50)
AS
SET NOCOUNT ON
SET XACT_ABORT ON
DECLARE @ErrorMessage varchar(1000)
BEGIN TRANSACTION
--Insert to Valuation
INSERT INTO [Valuation]
(
TrackingNumber,
FormMobiValuationId,
ValuationPropertyId, -- new
ValuationFileName,
Date,
ValuationStatus,
IsActive
)
VALUES
(
@TrackingNumber,
@FormMobiValuationId,
@ValuationPropertyId,--new
@ValuationFileName,
GETDATE(),
1, --Created
1
)
IF @@ERROR > 0
BEGIN
SET @ErrorMessage = 'Valuation Insert failed'
GOTO ErrorHandler
END
ELSE
BEGIN
COMMIT TRANSACTION
RETURN
END
ErrorHandler:
RAISERROR(@ErrorMessage,16,1);
ROLLBACK TRANSACTION
RETURN -1
<小时>
发生错误的C#调用,错误信息出现在最后一行.
C# call where error occurs, The error message appears on the last line.
public ObjectResult<Valuation> ValuationCreate(global::System.String trackingNumber, global::System.String formMobiValuationId, Nullable<global::System.Int32> valuationPropertyId, global::System.String valuationFileName)
{
ObjectParameter trackingNumberParameter;
if (trackingNumber != null)
{
trackingNumberParameter = new ObjectParameter("TrackingNumber", trackingNumber);
}
else
{
trackingNumberParameter = new ObjectParameter("TrackingNumber", typeof(global::System.String));
}
ObjectParameter formMobiValuationIdParameter;
if (formMobiValuationId != null)
{
formMobiValuationIdParameter = new ObjectParameter("FormMobiValuationId", formMobiValuationId);
}
else
{
formMobiValuationIdParameter = new ObjectParameter("FormMobiValuationId", typeof(global::System.String));
}
ObjectParameter valuationPropertyIdParameter;
if (valuationPropertyId.HasValue)
{
valuationPropertyIdParameter = new ObjectParameter("ValuationPropertyId", valuationPropertyId);
}
else
{
valuationPropertyIdParameter = new ObjectParameter("ValuationPropertyId", typeof(global::System.Int32));
}
ObjectParameter valuationFileNameParameter;
if (valuationFileName != null)
{
valuationFileNameParameter = new ObjectParameter("ValuationFileName", valuationFileName);
}
else
{
valuationFileNameParameter = new ObjectParameter("ValuationFileName", typeof(global::System.String));
}
return base.ExecuteFunction<Valuation>("ValuationCreate", trackingNumberParameter, formMobiValuationIdParameter, valuationPropertyIdParameter, valuationFileNameParameter);
}
推荐答案
该消息表示存储过程的结果不包含名为 ValudationId
的列.仔细检查您的 select
语句并在 SSMS 中运行它以确保您带回该列.
The message means that the results of the stored procedure do not contain a column named ValudationId
. Double check your select
statement and run it in SSMS to ensure that you're bringing back that column.
您的过程不包含 select
语句.您需要选择插入的标识值(例如,使用 scope_identity()
函数),以便 EF 可以将其映射回实体.
Your procedure does not contain a select
statement. You need to select the inserted identity value (using the scope_identity()
function, for example) so that EF can map it back to the entity.
例如
insert into Table
(
Col1,
Col2
)
values
(
1,
2
)
select scope_identity() as IdentityColName
另外,顺便说一句,您不需要插入语句中的所有事务;你只有一个修改数据的语句(你的插入).
Also, as an aside, you don't need all that transaction business in your insert statement; you only have one statement (your insert) that's modifying data.
这篇关于来自 EF 映射对象的不兼容数据读取器异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!