Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The data reader is incompatible with the specified model

I am running a stored procedure by adding it in existing model (edmx) by Add--> function Import.

I received the following error.

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

_Result class of the Modle is as follows

public partial class stored_procedure_Result
{
    public int UID { get; set; }
    public int SYSTEM_ID { get; set; }
    public byte ACTIVE { get; set; }
    public string LEVEL { get; set; }
    public string SYSTEM_CODE { get; set; }
    public string SYSTEM_NAME { get; set; }
    public Nullable<int> SYSTEM_SUB_TYPE { get; set; }
    public Nullable<int> PM_ID { get; set; }
}

Error occurred on the bottom line of this class

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
using Company.Product.Domain.Models;

namespace Company.Product.Domain.Data
{
    public partial class BusinessPartnerDataContext : DbContext
    {
        public BusinessPartnerDataContext()
            : base("name=BusinessPartnerDataContext")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }


        public virtual ObjectResult<stored_procedure_Result> stored_procedure(Nullable<int> pCId, Nullable<int> pSystemId, Nullable<bool> pParameterShow, Nullable<bool> pRETActive, Nullable<bool> pAllLevels, Nullable<bool> pSystemSubTypeShow, Nullable<bool> pShowResultSet)
        {
            ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(stored_procedure_Result).Assembly);

            var pCIdParameter = pCId.HasValue ?
                new ObjectParameter("pCId", pCId) :
                new ObjectParameter("pCId", typeof(int));

            var pSystemIdParameter = pSystemId.HasValue ?
                new ObjectParameter("pSystemId", pSystemId) :
                new ObjectParameter("pSystemId", typeof(int));

            var pParameterShowParameter = pParameterShow.HasValue ?
                new ObjectParameter("pParameterShow", pParameterShow) :
                new ObjectParameter("pParameterShow", typeof(bool));

            var pRETActiveParameter = pRETActive.HasValue ?
                new ObjectParameter("pRETActive", pRETActive) :
                new ObjectParameter("pRETActive", typeof(bool));

            var pAllLevelsParameter = pAllLevels.HasValue ?
                new ObjectParameter("pAllLevels", pAllLevels) :
                new ObjectParameter("pAllLevels", typeof(bool));

            var pSystemSubTypeShowParameter = pSystemSubTypeShow.HasValue ?
                new ObjectParameter("pSystemSubTypeShow", pSystemSubTypeShow) :
                new ObjectParameter("pSystemSubTypeShow", typeof(bool));

            var pShowResultSetParameter = pShowResultSet.HasValue ?
                new ObjectParameter("pShowResultSet", pShowResultSet) :
                new ObjectParameter("pShowResultSet", typeof(bool));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<stored_procedure_Result>("stored_procedure", pCIdParameter, pSystemIdParameter, pParameterShowParameter, pRETActiveParameter, pAllLevelsParameter, pSystemSubTypeShowParameter, pShowResultSetParameter);
        }
    }
}

Sored procedure is as follows

ALTER PROCEDURE STORED_PROCEDURE 
AS 
DECLARE @SYSTEMS TABLE (UID int NOT NULL IDENTITY(1,1),
                        SYSTEM_ID int NOT NULL,
                        ACTIVE tinyint NOT NULL,
                        [LEVEL] char(2) NOT NULL,
                        SYSTEM_CODE char(2) NULL,
                        SYSTEM_NAME varchar(50) NULL,
                        pm_ID int NULL,SYSTEM_SUB_TYPE int NULL)
INSERT INTO @SYSTEMS
VALUES (1,
        62,
        1,
        'LEVEL',
        'CODE',
        'NAME')
SELECT UID,
       SYSTEM_ID,
       ACTIVE,
       LEVEL,
       SYSTEM_CODE,
       SYSTEM_NAME
FROM @SYSTEMS RETURN 0

Result of the procedure is

UID         SYSTEM_ID     ACTIVE     LEVEL      SYSTEM_CODE     SYSTEM_NAME

 1           62           1         LEVEL               CODE           NAME

Some post suggested to remove the RET statement from the stored procedure, I tried with commenting the RET statement in the stored procedure as well , but it didn’t help

Some post suggested to check the column names, I cheked the column names in the complex type , the column names exactly matching

Some post suggested that the all the columns which are there in the _Result class should be RETed from the procedure, but the _Result class is automatically generated, however I tried removing the last two columns as well, but it didn’t help.. Moreover the error says ‘UID’ column, but that is present in the Procedure , procedure result, _Result class .

I am feeling that error is in some references

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
using Company.Product.Domain.Models;

as when the “Update modle from database”, it always throws error like “cannot convert System.Data.Object to System.Data.Entity.Core.Objects”

like image 295
user3489548 Avatar asked Jun 09 '14 18:06

user3489548


1 Answers

Please, what happens if you follow these steps?

1 - Open your Model end click Model Browser

Open your Model end click Model Browser

2 - Open Complex Types folder and delete your stored_procedure_Result

Open Complex Types folder and delete your stored_procedure_Result

3 - Open Stored Procedure folder and double click your procedure

Open Stored Procedure folder and double click your procedure

4 - Get Column Information

5 - Create New Complex Type

Get Column Information and Create New Complex Type

like image 62
Guilherme Fidelis Avatar answered Sep 20 '22 03:09

Guilherme Fidelis