无法隐式转换类型'System.Data.Entity.Core.Objects.ObjectResult<>' 'int'

时间:2017-04-10 11:53:39

标签: c# asp.net-mvc-5 entity-framework-6

我已经创建了一个存储过程并在MVC应用程序中使用Entity框架数据库第一种方法。我正在尝试捕获requestid,它是在我的数据库中在存储库方法中生成的标识字段。我在我的存储库方法中遇到编译时错误

  

错误CS0029无法隐式转换类型   'System.Data.Entity.Core.Objects.ObjectResult<CC.GRP.MCRequest.Models.spInsertRequestId_Result>'   至   'int'CC.GRP.MCRequest E:\ TFS2015 \ CC.GRP.MCRequest \ CC.GRP.MCRequest \ CC.GRP.MCRequest \ Repository \ RequestRepository.cs 127 Active

在Entity框架模型浏览器的函数导入部分,我将函数指向我创建的复杂类型。这个复杂类型包含int 32类型的RequestId。有人可以告诉我问题是什么

StoredProcedure的

ALTER PROCEDURE [dbo].[spInsertRequest]
(

             @CountryCode char(2), @CompanyCode varchar(4), @SalesOfficeID int,@CustomerMasterDataID int,@GeneralAgreementName varchar(150)
           , @IsDayBusiness bit, @ProjectName varchar(160),@ProjectContent  varchar(160),@ContractStartDate datetimeoffset(7)
           , @ContractDurationInMonths int,@Priority datetimeoffset(7),@PersonResponsibleMasterDataId int, @ProjectApplicantMasterDataId int
           , @IsRequiredToSendToCostDesk bit,@CalculationSpecials varchar(2000), @RequestStatusCode varchar(4)
           , @CreatedBy varchar(20)

)

AS
BEGIN

    set nocount on

    ----------------------------------------
    -- variables
    ----------------------------------------

    -- error
    declare @ErrorMessage nvarchar(2048), @ErrorSeverity int, @ErrorState int, @RequestId int

    ----------------------------------------
    -- insert record
    ----------------------------------------

    begin try


        -- insert record
            INSERT INTO [dbo].[Requests]
           ([CountryCode]
           ,[CompanyCode]
           ,[SalesOfficeID]
           ,[CustomerMasterDataID]
           ,[GeneralAgreementName]
           ,[IsDayBusiness]
           ,[ProjectName]
           ,[ProjectContent]
           ,[ContractStartDate]
           ,[ContractDurationInMonths]
           ,[Priority]
           ,[PersonResponsibleMasterDataId]
           ,[ProjectApplicantMasterDataId]
           ,[IsRequiredToSendToCostDesk]
           ,[CalculationSpecials]
           ,[RequestStatusCode]
           ,[CreatedBy]
           )
     VALUES
           (@CountryCode, 
            @CompanyCode, 
            @SalesOfficeID,
            @CustomerMasterDataID,
            @GeneralAgreementName,
            @IsDayBusiness, 
            @ProjectName, 
            @ProjectContent,
            @ContractStartDate,
            @ContractDurationInMonths, 
            @Priority,
            @PersonResponsibleMasterDataId,
            @ProjectApplicantMasterDataId,
            @IsRequiredToSendToCostDesk,
            @CalculationSpecials,
            @RequestStatusCode,
            @CreatedBy 
           )




        -- get assigned id
        set @RequestId = scope_identity()



    end try
    begin catch
        select @ErrorMessage = dbo.fnGetErrorMessage('Insert Request Record', error_message(), object_schema_name(@@procid), object_name(@@procid), error_line()), @ErrorSeverity = error_severity(), @ErrorState = error_state();
        raiserror(@ErrorMessage, @ErrorSeverity, @ErrorState);
        return
    end catch

    -- return assigned id
    select @RequestId as RequestId

END

存储库方法

public int CreateRequest(Request request)
{
    int requestID = 0;

    if (HttpContext.Current.Session["MCRSession"] != null)
    {
        requestID = db.spInsertRequest(    request.CountryCode,
                                           request.CompanyCode,
                                           request.SalesOfficeID,
                                           request.CustomerMasterDataID,
                                           request.GeneralAgreementName,
                                           request.IsDayBusiness,
                                           request.ProjectName,
                                           request.ProjectContent,
                                           request.ContractStartDate,
                                           request.ContractDurationInMonths,
                                           request.Priority,
                                           request.PersonResponsibleMasterDataId,
                                           request.ProjectApplicantMasterDataId,
                                           request.IsRequiredToSendToCostDesk,
                                           request.CalculationSpecials,
                                           request.RequestStatusCode,
                                           MCRHelper.GetShortname());
    }

    return requestID;
}

1 个答案:

答案 0 :(得分:1)

您的db.spInsertRequest方法会返回spInsertRequest_Result,而不是int。很可能,这个类是脚手架来自动处理SP的结果,只是有一个RequestId属性你可以访问:

 requestID = db.spInsertRequest(...).RequestId;

或者您可以简单地更改该方法以将查询结果提取到int,而不是spInsertRequest_Result。该方法可能具有以下内容:

Database.SqlQuery<spInsertRequest_Result>("EXEC spInsertRequest ...");

将其更改为:

Database.SqlQuery<int>("EXEC spInsertRequest ...");

然而,鉴于这是一个脚手架,如果你在某个时候被迫重新生成,那么这种改变可能不会持久,所以要小心。