Changing the select in SP gives error

Asked

Viewed 53 times

-2

That is the mistake:

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 it:

select nome from tabela --> returns the cited error

My model is like this:

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

SP call in 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 bank runs normally. How do I resolve this?

  • 3

    The error is saying that the query does not return the column id that is expected, try to include it in the query.

  • 3

    In doing SqlQuery<Cidade> is waiting for SP to return all the fields that make up that class, where, in this case, the column id is included.

  • If your model has two fields, you cannot return only one

1 answer

1

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

In your case your select must be: SELECT id, nome FROM tabela Entity will popular the class id and will not give error any.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.