Problem with Entity Framework Relationship

Asked

Viewed 404 times

2

I am having problems in the relationship of my bank, I am developing an application that manages Courses, I am still beginner in Asp.net MVC. I have two tables Pupil and Course, and I have another table that makes the association between these two tables Hallucinogenic.

Model Aluno

 public class Aluno
{
    [Key]
    public int Id { get; set; }

    public string Nome { get; set; }

    ...

    public virtual ICollection<AlunoCurso> AlunoCursos { get; set; } }

Model Curso

public class Curso
{
    [Key]
    public int Id { get; set; }

    public string Nome_Curso { get; set; }

    ...

    public virtual Aluno Aluno { get; set; }

    public ICollection<AlunoCurso> AlunoCursos { get; set; } }

Model Alunocurso

public class AlunoCurso
{
    [Key]
    public int Id { get; set; }

    public int AlunoId { get; set; }

    public int CursoId { get; set; }

    public bool Aprovado { get; set; }

    public virtual Aluno Aluno { get; set; }

    public virtual Curso Curso { get; set; }

}

The problem is that the column Alunoid on the table of Course is not receiving the Id student’s. Tabela Curso

But in the association table Hallucinogenic is receiving the value of the field Alunoid. inserir a descrição da imagem aqui

List of Student Courses

public ActionResult MeusCursos()
    {
        Aluno aluno = db.Alunos.FirstOrDefault(a => a.Usuario == User.Identity.Name);
        if (aluno != null)
            return View("MeusCursos", db.Cursos.ToList());

        return View();}

View Meuscursos

@model IEnumerable<MeuProjeto.Models.Curso>

@{
    Layout = "/Views/Shared/_Layout.cshtml";
}

<h2>Meus Cursos</h2>

<table class="table table-hover">
    <tr>
        <th>
            Curso
        </th>
        <th>
            Aluno
        </th>
        <th>
            Aprovado?
        </th>

        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Nome_Curso)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AlunoCursos)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Aprovado)
            </td>
            <td>
                <div class="btn-group">
                    <div class="col-md-offset-2 col-md-10">
                        @if (item.Aprovado == false)
                        {
                            <input type="submit" value="Pendente de Aprovação" name="meusCursos" class="cursos btn btn-success" disabled="disabled" data-id="@item.Id"/>
                        }
                        else
                        {
                            <input type="submit" value="Emitir Declaração" name="meusCursos" class="cursos btn btn-default" enable="enable" />
                        }
                    </div>
                </div>
            </td>
        </tr>
    }

</table>

Listing Screen inserir a descrição da imagem aqui

1 answer

3


This isn’t exactly a problem. It’s just a code scrawl that’s been left behind and hasn’t been removed.

See here:

public class Curso
{
    [Key]
    public int Id { get; set; }

    public string Nome_Curso { get; set; }

    ...


    public virtual Aluno Aluno { get; set; } // Este pode ser retirado

    public ICollection<AlunoCurso> AlunoCursos { get; set; } 
}

As now a student can be enrolled in several courses, and a course can have several students, this association:

public virtual Aluno Aluno { get; set; }

It has lost the sense of being there, and can be removed.

After removing, do not forget to generate a new Migration for the removal of the column properly.


EDIT

To Action is incorrect. If you want to read a student’s courses, simply select only the student and use the navigation properties:

public ActionResult MeusCursos()
{
    var aluno = db.Alunos.FirstOrDefault(a => a.Usuario == User.Identity.Name);
    if (aluno != null)
        return View("MeusCursos", aluno);

    return View();
}

Notice that I changed @model of View:

@model IEnumerable<MeuProjeto.Models.Aluno>

@{
    Layout = "/Views/Shared/_Layout.cshtml";
}

<h2>Meus Cursos</h2>

<table class="table table-hover">
    <tr>
        <th>
            Curso
        </th>
        <th>
            Aprovado?
        </th>

        <th></th>
    </tr>

    @foreach (var item in Model.AlunoCursos)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Curso.Nome_Curso)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Aprovado)
            </td>
            <td>
                <div class="btn-group">
                    <div class="col-md-offset-2 col-md-10">
                        @if (item.Aprovado == false)
                        {
                            <input type="submit" value="Pendente de Aprovação" name="meusCursos" class="cursos btn btn-success" disabled="disabled" data-id="@item.Id"/>
                        }
                        else
                        {
                            <input type="submit" value="Emitir Declaração" name="meusCursos" class="cursos btn btn-default" enable="enable" />
                        }
                    </div>
                </div>
            </td>
        </tr>
    }

</table>
  • But what happens @Gypsy is when I list the courses that the student is enrolled, the name of the student comes with nothing, because of this Alunoid.

  • @Newbie And you’re listing that name as?

  • I edited the question @Gypsy is coming so blank the field. When I put the number of the Alunoid on the table Course on the bench, he filled the field Pupil in View , but doing so came all student data and not only his name, understood?!

  • @Newbie Edited the answer.

  • Gave error @Gypsy, this mine Action is from the controller Course. I made that change you indicated but in mine View gives error in this line also @foreach (var item in Model.Alunocursos), it does not recognize the Alunocursos. I want to list the Course, The Name of the Student, and Approved.

  • @Newbie You’re using wrong. Curso has to do with AlunoCursos, and AlunoCurso has to do with Aluno. If you want to list the courses of a Aluno, you have to select the Aluno and use the properties virtual of Aluno, even if it is a Controller course.

  • So this one of mine Action goes inside the controller Pupil @Gypsy ?

  • @Newbie It’d be better if it was.

  • Fine, I’ll try to solve according to your reply @Gypsy. If I can’t open another detailing better?

  • @Yes, of course! Make yourself at home.

Show 5 more comments

Browser other questions tagged

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