Changing the select in SP gives error [closed]

-2

This is the error:

System.Data.Entity.Core.EntityCommandExecutionException: 'The data reader is incompatible with the specified 'TreinamentoCrudApi.Context.Cidade'. A member of the type, 'id', does not have a corresponding column in the data reader with the same name.'

If in SP I put one:

select * from tabela - > works

But if I do this:

select nome from tabela - > returns the error quoted

My model looks like this:

public class Cidade
    {
        [Key]
        public int id { get; set; }
        [Required]         
        public String nome { get; set; }
    }

Calling the SP in the API

public class GetCidade
    {
        BancoContext banco = new BancoContext();  
        public List<Cidade> GetCidades()
        {
            return banco.Database.SqlQuery<Cidade>("exec sp_cons_cidade").ToList();
        }
    }

My proc in the database runs normally. How do I resolve this?

    
asked by anonymous 10.08.2018 / 14:59

1 answer

1

Your class has the id property decorated as Key so it is required. The entity expects the query to return all properties that are not optional

In your case your select has to be: SELECT id, nome FROM tabela the entity will populate the class id and will not make any errors.

    
14.08.2018 / 19:02