How, after a POST request, to insert values into another table automatically with the Entity Framework

Asked

Viewed 37 times

1

Good night! I have the following question: I have 3 tables (Students, Etapasala, Classroom) inserir a descrição da imagem aqui

As you can see, Etapasala is a connecting table between the student and the classroom. I’m using the MVC structure with Entity Framework, I’ve crud the 3 tables and everything works correctly. The problem is that I did not want to have to go in the View of Etapasala to link a student in your class at a certain stage and nor put a dropdown in the student’s registration for such, I would like to do this automatically at the time that POST request the student, link it to a room in step 1 and another room in step 2.

Follow the code of the POST method of Alunoscontroller:

// POST: Alunos/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("IdAluno,Nome,Sobrenome")] Aluno aluno)
    {
        if (ModelState.IsValid)
        {
            _context.Add(aluno);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(aluno);
    }

Model de Etapasala:

public partial class EtapaSala
{ 

    public int IdEtapa { get; set; }
    public int? Etapa { get; set; }
    public int? IdAluno { get; set; }
    public int? IdSala { get; set; }

    public virtual Aluno IdAlunoNavigation { get; set; }
    public virtual Sala IdSalaNavigation { get; set; }
}
  • 1

    if the tables are correctly listed in the code, the EF will cascade into the 3 tables if necessary

  • What do you mean? I think you do not understand the question, the tables are related, so much so that when I go in the view Etapasala has the ids of students and classrooms to choose and register normally, but that’s not what I want, I wanted to create the Student, already do the insert in Etapasala Automatically, without having to go on any screen or select nd in the student’s registration.

  • 1

    First on the registration screen of Student has to have more information of the Room! in the request itself has that value or room values, when clicking the button in the code inserts the student and then the room that belongs to it. Have an unnecessary thing id Step only the two keys are required.

  • 1

    I think you who did not understand...if everything is related and right in the EF just fill all the objects and make a single save in the bank (Applychanges). If it is related should have Aluno.EtapaSala and in EtapaSala.Sala, just set all values and insert at a single time, or see everything filled as @novic commented

  • Sorry Ricardo, I had not really understood, thank you! Really then my modeling has some mistakes then right? as the novic mentioned, the stage id is unnecessary and I need to have a room field in students, but what about stage field? because it will be two steps and one step at a time, half of one room will change places with the other, so it would need to be already pre-registered the classrooms that each student will be in each stage.

  • I believe that in its modeling it lacked to include Idetapa as this in the class. But it would be necessary to review its modeling so that create this process.

Show 1 more comment
No answers

Browser other questions tagged

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