EF - SqlQuery - not have a corresponding column in the data reader with the same name

.net c# entity-framework entity-framework-6 mysql

Question

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

1
0
8/11/2017 2:00:01 AM

Accepted Answer

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}
1
8/11/2017 5:33:07 AM


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