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

Asked

Viewed 2,409 times

4

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

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

DER da aplicação

This table, as its name says, is designed to store the data of users who will be part of some project, or several.

By creating my edmx in my project of Visual Studio, the table Equipe_projeto, is not created in the model classes, and only the relationship goes in the edmx, as in the image below:

Diagrama edmx da aplicação

So I come across the question of how I’m going to popular my Equipe_projeto? I’m a little lost here in how I will relate my users to the projects, using Asp.net MVC?

In the Create method code of the project I have the following code, and in this there is nothing that relates the table Equipe_projeto:

// 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 many relationship for many, so that I can insert data into more than one table at the same time, in my case in the table Project, and Equipe_projeto, thus defining the users who are part of the project.

Note: I have a great experience already with projects in Webforms, but now I’m going after learning in Asp.net mvc, Entity framework and webapi.

1 answer

5


The creation of tables by EDMX causes the Entity Framework to configure this associative table by Fluent API, within your context file (a class that derives DbContext). In my experience, I don’t recommend using it this way for three reasons:

  • It is unclear how and when the Entity Framework will make the association for you;
  • Membership cannot 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.

The ideal would be for you to create the Model Equipe_Projeto manually, associating Projeto and Equipe as 1 to N. This avoids confusion.

Also, I do not recommend you continue long in this approach with the EDMX file. With the use of Attributes, this approach quickly becomes obsolete.

  • Thanks @Ciganomorrisonmendez, thank you. Following your advice, I will create the model manually and make the association. After that, I change the controller of the Project so that it must also "see" this association, thus inserting in the database also the users that are part of the project. And what way would you recommend me on the issue of using edmx Attributes?

  • 2

    @Ericosouza Using Attributes in EDMX is very complicated. I would make the entire statement of Models textually, by class. It may not be intuitive at first, but the gain you get with validation and business rules with this kind of discipline is worth all the effort.

  • @Ciganomorrizonmendez, currently using metadata classes, I have Attributes, and validation and rules in it and not in EDMX. You believe this approach is valid?

  • 1

    @Ericosouza Yes, they are the best practices in ASP.NET MVC and EF until then.

Browser other questions tagged

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