System.NullReferenceException. How to include fields from another table?

Asked

Viewed 54 times

-1

Friends, I had a table Pessoa that contained a lot of user information. I decided to create a table Aluno to separate the information. My tables are like this:

Person: Cod, Nome, Email, Senha
Pupil: Cod, Status, Data_Registro, PessoaId

I am trying to get a list of requests from the connected user. However, my table Pedidos has a key to the table Alunos, not for the table Pessoa. The information of Aluno is null. How can I include it in the table Pessoa?

Error:

System.NullReferenceException: 
  'Object reference not set to an instance of an object.'

LojaVirtual.Models.pessoa.aluno.get retornou null.

Pedidocontroller.Cs

public IActionResult Index(int pagina)
{
    pessoa pessoa = _loginPerson.GetPessoa();
    var requests= _requestRepository.ObterTodosPedidoPessoa(pagina, pessoa.aluno.Cod);

    return View(pedidos);
}

Médoto Getpessoa()

public pessoaGetPessoa()
{
    //Deserializar
    if( _sessao.Existe(Key)) {
        string pessoaJSONString = _sessao.Consultar(Key) ;
        return JsonConvert.DeserializeObject<pessoa>(pessoaJSONString);

    }
    else
    {
        return null;
    }
}

Method Consult

public string Consultar(string Key)
{
    return _context.HttpContext.Session.GetString(Key);
}

I thank you for any comment!

1 answer

0

If I understand correctly, the problem is in the fact of having a Pessoa but not having a Aluno, since the key to Pedidos is connected to the Aluno. The only way I can see to make that call would be to access the property Cod which is present in both entities, if they have the same value for the records in the tables Aluno and Pessoa:

var requests = _requestRepository.ObterTodosPedidoPessoa(pagina, pessoa.Cod);

If the record Cod is not the same for Aluno and Pessoa, see as a solution the "direct link" between Pessoa and Pedido, since, if understood correctly, there is always a Pessoa but not always this Pessoa is a Aluno.

  • Thanks for the feedback. The Student is always a person. When I register a person in the "Person" table, the "Student" table is also filled with a fk for "Person". I can not put a key "Orders" for "Person", because another system also makes queries in the bank. The solution would be to actually enter the student information. But I don’t understand how I can do this!

  • @Edrianbiagi as I do not know which library is using to do the rescue and insertion of data, I have no way to complement the answer with code currently, but I believe only creating a method that receives the ID of a person and the information of Aluno to insert into the repository or by doing Insert manually, since the property Cod is also not the same for Aluno and Pessoa. Also check that there is no problem with how you are rescuing the entity Aluno since there is a previous Index in the table as described.

Browser other questions tagged

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