0

I'm using entity framework and I have this class

public partial class TipoRequisicaoSubCategoria
    {
        public TipoRequisicaoSubCategoria()
        {
            this.Formularios = new HashSet<Formulario>();
        }

        public int Id { get; set; }
        public int IdTipoRequisicao { get; set; }
        public int IdSubcategoria { get; set; }

        public virtual ICollection<Formulario> Formularios { get; set; }
        public virtual subcategoria Subcategoria { get; set; }
        public virtual TipoRequisicao TipoRequisicao { get; set; }
    }

The table is

Table: tipo_requisicao_subcategoria
Columns:
ID_TIPO_REQUISICAO_SUBCATEGORIA int(11) AI PK 
ID_TIPO_REQUISICAO int(11) 
ID_SUBCATEGORIA

If I do a search using linq, the results are perfect, but when I tryng to do a search using SqlQuery method, I get the message:""The data reader is incompatible with the specified 'medicEntities.TipoRequisicaoSubCategoria'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name."

Here is the query that I'm using:

return _context.tipo_requisicao_subcategoria.SqlQuery(string.Format(@"SELECT TS.* FROM tipo_requisicao_subcategoria TS INNER JOIN 
                                                                                                          tipo_requisicao T ON T.ID_TIPO_REQUISICAO = TS.ID_TIPO_REQUISICAO 
                                                                                                          WHERE T.ID_ESPECIALIDADE = {0}", idEspecialidade)).ToList();

Ps: I'm using database first

  • First, check table mapping details in EDMX file. Is that `ID_TIPO_REQUISICAO_SUBCATEGORIA` already mapped to `Id` property on generated table class? Or maybe non-mapped/mapped to another property instead? – Tetsuya Yamamoto Aug 11 '17 at 02:51
  • Possible duplicate of [Incompatible Data Reader Exception From EF Mapped Objects](https://stackoverflow.com/questions/14235783/incompatible-data-reader-exception-from-ef-mapped-objects) – Michael Freidgeim Dec 20 '18 at 02:02

1 Answers1

2

The message means that the results of the SQL Query do not contain a column named Id. In this example, I changed the columns name to "Id", "IdTipoRequisicao" and "IdSubcategoria" respectively.

SELECT 
TS.ID_TIPO_REQUISICAO_SUBCATEGORIA as Id,
TS.ID_TIPO_REQUISICAO as IdTipoRequisicao,
TS.ID_SUBCATEGORIA as IdSubcategoria
FROM tipo_requisicao_subcategoria TS INNER JOIN
tipo_requisicao T ON T.ID_TIPO_REQUISICAO = TS.ID_TIPO_REQUISICAO
WHERE T.ID_ESPECIALIDADE = {0}
Lee Derting
  • 146
  • 1
  • 4