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.