S
S
Saharman2018-03-24 00:02:15
ASP.NET
Saharman, 2018-03-24 00:02:15

How to store many-to-many data?

Unable to save data in many-to-many relationship. There are three tables: Person, Project and ProjectPerson (an intermediate table).
Here is the implementation of the Project model:

public partial class Project
    {
        public Project()
        {
            ProjectPerson = new HashSet<ProjectPerson>();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime IncomeDate { get; set; }
        public int? LeaderId { get; set; }
        public Person Leader { get; set; }
        public ICollection<ProjectPerson> ProjectPerson { get; set; }
    }
}

Implementation of the Person model:
public partial class Person
    {
        public Person()
        {
            Project = new HashSet<Project>();
            ProjectPerson = new HashSet<ProjectPerson>();
        }

        public int Id { get; set; }
        public string FullName { get; set; }
        public string PhoneNumber { get; set; }
        public string Email { get; set; }
        public string Vk { get; set; }
        public string Instagram { get; set; }
        public string Facebook { get; set; }
        public string Twitter { get; set; }
        public ICollection<Project> Project { get; set; }
        public ICollection<ProjectPerson> ProjectPerson { get; set; }
    }
}

Implementation of the ProjectPerson model:
public partial class ProjectPerson
    {
        public int ProjectId { get; set; }
        public int PersonId { get; set; }
        public Person Person { get; set; }
        public Project Project { get; set; }
    }

Optional DBcontext:
modelBuilder.Entity<Person>(entity =>
            {
                entity.ToTable("person");

                entity.Property(e => e.Id).HasColumnName("ID");

                entity.Property(e => e.Email)
                    .HasColumnName("email")
                    .HasMaxLength(200);

                entity.Property(e => e.Facebook)
                    .HasColumnName("facebook")
                    .HasMaxLength(200);

                entity.Property(e => e.FullName)
                    .IsRequired()
                    .HasColumnName("full_name")
                    .HasMaxLength(200);

                entity.Property(e => e.Instagram)
                    .HasColumnName("instagram")
                    .HasMaxLength(200);

                entity.Property(e => e.PhoneNumber)
                    .HasColumnName("phone_number")
                    .HasMaxLength(50);

                entity.Property(e => e.Twitter)
                    .HasColumnName("twitter")
                    .HasMaxLength(200);

                entity.Property(e => e.Vk)
                    .HasColumnName("vk")
                    .HasMaxLength(200);
            });
 modelBuilder.Entity<Project>(entity =>
            {
                entity.ToTable("project");

                entity.Property(e => e.Id).HasColumnName("id");

                entity.Property(e => e.Description)
                    .IsRequired()
                    .HasColumnName("description")
                    .HasColumnType("text");

                entity.Property(e => e.IncomeDate)
                    .HasColumnName("incomeDate")
                    .HasColumnType("date");

                entity.Property(e => e.LeaderId).HasColumnName("leader_id");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasColumnName("name")
                    .HasColumnType("nchar(50)");

                entity.HasOne(d => d.Leader)
                    .WithMany(p => p.Project)
                    .HasForeignKey(d => d.LeaderId)
                    .HasConstraintName("FK_project_person");
            });
modelBuilder.Entity<ProjectPerson>(entity =>
            {
                entity.HasKey(e => new { e.ProjectId, e.PersonId });

                entity.ToTable("project_person");

                entity.Property(e => e.ProjectId).HasColumnName("project_id");

                entity.Property(e => e.PersonId).HasColumnName("person_id");

                entity.HasOne(d => d.Person)
                    .WithMany(p => p.ProjectPerson)
                    .HasForeignKey(d => d.PersonId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_project_person_person");

                entity.HasOne(d => d.Project)
                    .WithMany(p => p.ProjectPerson)
                    .HasForeignKey(d => d.ProjectId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_project_person_project");
            });

Here is the controller:
[HttpGet]
        public IActionResult Create()
        {
            ViewBag.Persons = new SelectList(_context.Person.ToList(), "Id", "FullName");
            return View();
        }
[HttpPost]
        public IActionResult Create(Project project)
        {
            _context.Project.Add(project);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }

And here is the view:
<form class="form" asp-action="Create" asp-controller="Project" method="post" enctype="multipart/form-data">
                    <div class="form-group row">
                        <label class="col-md-12" asp-for="Name">Название проекта</label>
                        <div class="col-md-12">
                            <input type="text" class="form-control"  asp-for="Name">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-md-12" asp-for="Description">Описание</label>
                        <div class="col-md-12">
                            <textarea class="form-control" rows="4" asp-for="Description"></textarea>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-12" asp-for="LeaderId">Лидер</label>
                        <div class="col-sm-12">
                            <select class="custom-select col-12" id="inlineFormCustomSelect" asp-for="LeaderId" asp-items="ViewBag.Persons"><option selected="selected" disabled="disabled">Выберите руководителя</option></select>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label class="col-sm-12" asp-for="ProjectPerson">Команда</label>
                        <div class="col-sm-12">
                            <select class="custom-select col-12" id="inlineFormCustomSelect" asp-for="ProjectPerson" asp-items="ViewBag.Persons" multiple><option selected="selected" disabled="disabled">Выберите руководителя</option></select>
                        </div>
                    </div>
                   <input type="submit" value="Добавить" class="btn btn-common">
                </form>

I want to note that the models and the context were not written by me, they are created by themselves from the MSQL database

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Kuznetsov, 2018-03-24
@Saharman

In your case - you just need to add a ProjectPerson with appropriate values ​​to project or person. And save.
In general, you need to remove it and configure the model like this:

modelBuilder.Entity<Project>()
                .HasMany<Person>(s => s.Persons)
                .WithMany(c => c.Projects)
                .Map(cs =>
                        {
                            cs.MapLeftKey("project_id");
                            cs.MapRightKey("person_id");
                            cs.ToTable("project_person");
                        });

Additionally:
  • A general example is www.entityframeworktutorial.net/code-first/configu...
  • Approximately your case, only Code First: https://stackoverflow.com/questions/7050404/create...
  • Approximately your case: https://stackoverflow.com/questions/35527175/entit...
Important: All code examples are for EF6 only, EF Core cannot work without an intermediate entity.

E
eRKa, 2018-03-24
@kttotto

EF itself creates intermediate tables in a many-to-many relationship. You just need to specify this relationship between the Project and Person tables. There is no need to make them by hand.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question