Search information in more than one table

Asked

Viewed 78 times

2

In my project I currently have 3 tables: Students, Occurrences and Users. In them I have the views and the controllers.

So far, so good. My relationships are: Allunos&occurrences(1,N) / Occurrences & users(N,1).

My problem is that in action Details, both of Occurrences and of Users, I can not show some relationship information. In the case of occurrences I cannot show the name of the student or the user (but in the Index, I can show these names), and in the case of users I cannot show the name of the student (which would have to be shown by the fact that it comes from the relationship of Alunos&occurrences).

Here’s my code that’s on action which shows all information, except those mentioned above:

Actionresult of Controller Occurrences

    public ActionResult Detalhes(long? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        Ocorrencia ocorrencia = db.Ocorrencias.Find(id);

        if (ocorrencia == null)
        {
            return HttpNotFound();
        }
        return View(ocorrencia);
    }

Actionresult of Controller Users

    public ActionResult Detalhes(long? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        //Usuario usuario = db.Usuarios.Find(id);
        Usuario usuario = db.Usuarios.Include(o => o.Ocorrencias).AsNoTracking().FirstOrDefault(u => u.UsuarioID == id);

        if (usuario == null)
        {
            return HttpNotFound();
        }
        return View(usuario);
    }

Could someone help me ? This is the case of a Join ?

1 answer

2


Not use Find. Find brings only the requested registration, without the relationships between the other entities.

Change to FirstOrDefault, and use Include to load related data:

var ocorrencia = db.Ocorrencias
                 .Include(o => o.Aluno)
                 .Include(o => o.Usuario)
                 .FirstOrDefault(o => o.OcorrenciaID == id);

In the case of Usuario, don’t need to use AsNoTracking(). The rest is correct:

var usuario = db.Usuarios.Include(o => o.Ocorrencias).FirstOrDefault(u => u.UsuarioID == id);

Don’t forget to include the using System.Data.Entity; under the heading of Controllers.

  • Gypsy, in the case of occurrences worked well, but in the case of the user did not work. Just to be clear, the student only relates to the occurrence and the occurrence relates to both students and users. So, how can I get the name of the student who is on the occurrence and show him when fetching user details ?

  • @Érikthiago The occurrence would be @ocorrencia.Usuario.Nome.

  • That way I know Gypsy. But how the name of the student and show in the table of users ? I think I was clearer.

  • @Érikthiago Actually it was not very clear, but I think I understood. If you want to carry the student’s name from the occurrence, it is @ocorrencia.Aluno.Nome.

  • Ok. What happens is that when I detail the occurrences related to a login the student’s name is not shown. And I wanted to show the student’s name in the user details view. Because I want to show my user the occurrences that he generated and for whom, understood ? This is what is happening.

  • 1

    Then you use db.Usuarios.Include(u => u.Ocorrencias.Select(o => o.Aluno)).

  • Very good Gypsy, it worked here ! Thanks a lot !

Show 2 more comments

Browser other questions tagged

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