Cannot implicitly convert type 'System.Data.Entity.Core.Objects.ObjectResult<>' to 'int'

asp.net-mvc-5 c# entity-framework-6

Question

I have created a stored procedure and using Entity framework database first approach in MVC application. I am trying to capture the requestid which is an identity field that is generated in my database in the repository method. I am getting a compile time error in my repository method saying

Error CS0029 Cannot implicitly convert type 'System.Data.Entity.Core.Objects.ObjectResult<CC.GRP.MCRequest.Models.spInsertRequestId_Result>' to 'int' CC.GRP.MCRequest E:\TFS2015\CC.GRP.MCRequest\CC.GRP.MCRequest\CC.GRP.MCRequest\Repository\RequestRepository.cs 127 Active

In the function imports section of Entity framework model browser, I am pointing the function to the complex type that I have created. This complex type contains RequestId of type int 32. Could somebody tell me what the problem is

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

Repository method

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
4/10/2017 12:17:26 PM

Popular Answer

Your db.spInsertRequest method returns spInsertRequest_Result, not int. Likely, this class was scaffolded to handle the result of the SP automatically, and simply has a RequestId property you could access:

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

Or you can simply alter that method to fetch the query result into an int, rather than spInsertRequest_Result. The method likely has something like:

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

Change that to:

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

However, given that this is a scaffold, that change may not persist if you are forced to regenerate this at some point, so be careful there.

1
4/10/2017 1:42:20 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow