不兼容的数据读取器异常来自EF映射的对象 [英] Incompatible Data Reader Exception From EF Mapped Objects

查看:1274
本文介绍了不兼容的数据读取器异常来自EF映射的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架的工作,并已更新的表和它的存储过程,但我得到的时候调用存储过程以下错误。


  

数据读取器与指定不兼容
  FormValueModel.Valuation。该类型的成员,ValuationId',不
  不具有相应的列中具有相同名称的数据读取器


ValuationId是我的主键巫婆我想自动递增。

我可以执行存储过程从SQL Management Studio中发现,当我跑我的应用程序写入到数据库中,但随后出现的错误消息。

我不熟悉的实体框架的工作,只是有基础知识,我认为这可能是从model.edmx地图问题。

什么是在模型重建和映射表和存储过程的正确步骤?


存储过程。

  ALTER PROCEDURE [DBO]。[ValuationCreate]
    @TrackingNumber VARCHAR(100)
    @FormMobiValuationId VARCHAR(100)
    @ValuationPropertyId INT,
    @ValuationFileName VARCHAR(50)如SET NOCOUNT ON
SET XACT_ABORT ON
DECLARE @ErrorMessage VARCHAR(1000)BEGIN TRANSACTION
    --insert估值
    INSERT INTO [估价]
    (
        追踪号码,
        FormMobiValuationId,
        ValuationPropertyId - 新
        ValuationFileName,
        日期,
        ValuationStatus,
        IsActive
    )
    VALUES
    (
        @追踪​​号码,
        @FormMobiValuationId,
        @ValuationPropertyId - 新
        @ValuationFileName,
        GETDATE()
        1,--Created
        1
    )IF @@ ERROR> 0
开始
    SET @ErrorMessage ='估值插入失败
    GOTO的ErrorHandler
结束
其他
开始
    COMMIT TRANSACTION
    返回
结束的ErrorHandler:RAISERROR(@的ErrorMessage,16,1);
ROLLBACK TRANSACTION
返回-1


发生错误C#调用,出现在最后一行的错误消息。

 公共ObjectResult<估值及GT; ValuationCreate(全球:: System.String trackingNumber,全球:: System.String formMobiValuationId,可为空<全球:: System.Int32> valuationPropertyId,全球:: System.String valuationFileName)
        {
            ObjectParameter trackingNumberParameter;
            如果(trackingNumber!= NULL)
            {
                trackingNumberParameter =新ObjectParameter(TrackingNumber,trackingNumber);
            }
            其他
            {
                trackingNumberParameter =新ObjectParameter(TrackingNumber的typeof(全球:: System.String));
            }            ObjectParameter formMobiValuationIdParameter;
            如果(formMobiValuationId!= NULL)
            {
                formMobiValuationIdParameter =新ObjectParameter(FormMobiValuationId,formMobiValuationId);
            }
            其他
            {
                formMobiValuationIdParameter =新ObjectParameter(FormMobiValuationId的typeof(全球:: System.String));
            }            ObjectParameter valuationPropertyIdParameter;
            如果(valuationPropertyId.HasValue)
            {
                valuationPropertyIdParameter =新ObjectParameter(ValuationPropertyId,valuationPropertyId);
            }
            其他
            {
                valuationPropertyIdParameter =新ObjectParameter(ValuationPropertyId的typeof(全球:: System.Int32));
            }            ObjectParameter valuationFileNameParameter;
            如果(valuationFileName!= NULL)
            {
                valuationFileNameParameter =新ObjectParameter(ValuationFileName,valuationFileName);
            }
            其他
            {
                valuationFileNameParameter =新ObjectParameter(ValuationFileName的typeof(全球:: System.String));
            }            返回base.ExecuteFunction<估值与GT(ValuationCreate,trackingNumberParameter,formMobiValuationIdParameter,valuationPropertyIdParameter,valuationFileNameParameter);
        }


解决方案

该消息意味着存储过程的结果中不包含指定的列 ValudationId 。仔细检查你的选择语句和SSMS运行它,以确保你带回该列。

编辑:您的程序不包含一个选择语句。您需要选择插入的标识值(使用 SCOPE_IDENTITY()函数,例如),使EF可以映射回实体。

例如,

 插入表

    COL1,
    COL2



    1,
    2
)选择SCOPE_IDENTITY()作为IdentityColName

另外,顺便说一句,你并不需要所有的交易业务在你的INSERT语句;你只有一个声明(您的插入)这就是修改数据。

I am using entity frame work and have updated a table and its stored procedure but am getting the following error when the stored procedure is called.

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 is my primary key witch i want to auto increment.

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.

I'm unfamiliar with entity frame work 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?


Stored procedure.

    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# 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);
        }

解决方案

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.

EDIT: 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.

For example,

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆