Problem with relationship Entity Framework

2

I am having problems in my bank relationship, I am developing an application that manages Courses, I am still a beginner in ASP.NET MVC. I have two Student tables and Course tables, and I have another table that links those two StudentCourse tables.

Model Student

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

    public string Nome { get; set; }

    ...

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

Model Course

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 StudentCourse

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 StudentId column in the Course table table is not receiving the student's Id .

ButintheStudentCoursemembershiptableyouaregettingthevalueoftheStudentIdfield.

Student Courses List

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 My Courses

@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

    
asked by anonymous 12.06.2015 / 06:27

1 answer

3

This is not exactly a problem. It's just a code snippet left behind that has not 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 a student can now 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 withdrawn.

After removing, be sure to generate a new Migration to properly remove the column.

EDIT

Action is incorrect. If you want to read a student's courses, simply select 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've changed% 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>
    
12.06.2015 / 07:55