Tolistasync() - Sqlnullvalueexception: Data is Null. This method or Property cannot be called on Null values

Asked

Viewed 1,690 times

5

I’m getting the following error while trying to list all the records of a model:

Sqlnullvalueexception: Data is Null. This method or Property cannot be called on Null values. System.Data.Sqlclient.SqlBuffer.get_Int32()

My method:

//GET: api/empresa
[HttpGet]
public async Task<ActionResult<IEnumerable<Empresa>>> getEmpresas()
{
    return await _context.Empresas.ToListAsync();
}

My model:

public class Empresa
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Cep { get; set; }
    public string Logradouro { get; set; }
    public string Complemento { get; set; }
    public int Numero { get; set; }
    public string Bairro { get; set; }
    public string Localidade { get; set; }
    public string UF { get; set; }
    public int Unidade { get; set; }
    public int IBGE { get; set; }
    public int GIA { get; set; }
    public string Telefone{get;set;}
}

In my comic this is the columns and the result of selecting all the records:

So I researched this error happens when you have a non-null column in the bd, but in this case only the Id (primary key) is non-null, but it is already auto-incremented. inserir a descrição da imagem aqui

1 answer

6


The error that is occurring from get_Int32 must be why a field int in the table Company is null and in class Empresa no property int is as null.

Try to put int? Numero, int? Unidade and so on.

Browser other questions tagged

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