Update data that has many-to-many relationship with Entity Framework

0

I'm trying to update an object and the only thing that does not update is its relationship from many to many the rest as first and last name update

    [HttpPost]
    public IActionResult AtualizarProfessor(int id, string nome, string sobrenome, List<int> listaDeIdDasTurmas)
    {

        Professor professor = new Professor(nome, sobrenome);
        professor.Id = id;
        if (listaDeIdDasTurmas == null || listaDeIdDasTurmas.Count() == 0)
            return View();

        foreach (var turmaId in listaDeIdDasTurmas)
        {
            Turma turma = _turmaRepository.ObterTurmaPeloId(turmaId);

            if (turma == null || turma.Id == 0)
                return View();

            var professorturma = new ProfessorTurma();

            professorturma.ProfessorId = professor.Id;
            professorturma.TurmaId = turma.Id;

            professor.Turmas.Add(professorturma);
        }

        if (!professor.ValidaProfessor())
        {
            ViewData["Message"] = "Envie os dados do professor de forma correta!";
            return View();
        }
        else
        {
            _professorRepository.Atualizar(professor);
        }

        return RedirectToAction("cadastrarprofessor", "Admin");
    }




    public void Atualizar(Professor professor)
    {
        using (var contexto = new MinosContext())
        {
            contexto.Professores.Update(professor);
            contexto.SaveChanges();
        }
    }
    
asked by anonymous 10.11.2018 / 17:58

0 answers