Many-to-many relationship with Asp.net MVC and Entity Framework

4

I'm creating a project to study Asp.net MVC 5 along with Entity Framework 6 , and I came across a big question in a many-to-many relationship . I used model generated from the database, and I created the controllers and views automatically by Visual Studio .

In my database I have the many-to-many relationship between the User table and Projects table, which results in the Project_Path table as follows the image below:

Thistable,asitsnamesays,isdesignedtostoreuserdatathatwillbepartofsomeproject,orseveral.

WhencreatingmyedmxinmyVisualStudioproject,theTeam_Projecttableisnotcreatedinthemodelsclasses,andonlytherelationshipgoesinedmx,asintheimagebelow:

So I'm faced with the question of how I'm going to populate my Project_Table table? I'm a little lost here on how I will relate my users to the projects, using ASP.NET MVC?

In the project's Create method code I have the following code, and in this there is nothing that relates to the Project_Profile table:

// GET: Projeto/Create
public ActionResult Create()
{
    ViewBag.prioridade = new SelectList(db.prioridade, "prioridade1", "prioridade1");
    ViewBag.status = new SelectList(db.status, "status1", "status1");
    ViewBag.responsavel = new SelectList(db.usuario, "id", "nome");
    return View();
}

// POST: Projeto/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "id,nome,descricao,data_inicio,responsavel,prioridade,status")] projeto projeto)
{
    if (ModelState.IsValid)
    {
        db.projeto.Add(projeto);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    ViewBag.prioridade = new SelectList(db.prioridade, "prioridade1", "prioridade1", projeto.prioridade);
    ViewBag.status = new SelectList(db.status, "status1", "status1", projeto.status);
    ViewBag.responsavel = new SelectList(db.usuario, "id", "nome", projeto.responsavel);
    return View(projeto);
}

The big question is this, how to work with the many-to-many relationship, so that I can insert data into more than one table at a time, in my case in the Project table, strong> Project_Professor , thus defining the users who are part of the project.

Note: I have a great experience already with projects in WebForms, however I am now going behind the learning in asp.net mvc, entity framework and webapi.

    
asked by anonymous 13.09.2014 / 01:32

1 answer

5

The creation of tables by EDMX causes the Entity Framework to configure this associative table by Fluent API , within its context file (a class that derives DbContext ). From experience, I do not recommend using this way for three reasons:

  • It's not clear how and when the Entity Framework will make the association for you;
  • The association can not be extended, receive more properties, rules, etc.
  • Your example already shows that there is no Scaffolding (automatic prototyping) of the association, and it has to be done manually.

Ideally, you should create the Model Equipe_Projeto manually, associating Projeto and Equipe with 1 to N. This avoids confusion.

In addition, I do not recommend you continue to spend too much time in this approach with the EDMX file. Using Attributes , this approach quickly becomes obsolete.

    
13.09.2014 / 21:02