Insert/Update Many to Many Entity Framework . How do I do it?
Try this one for Updating:
[HttpPost]
public ActionResult Edit(Models.MathClass mathClassModel)
{
//get current entry from db (db is context)
var item = db.Entry<Models.MathClass>(mathClassModel);
//change item state to modified
item.State = System.Data.Entity.EntityState.Modified;
//load existing items for ManyToMany collection
item.Collection(i => i.Students).Load();
//clear Student items
mathClassModel.Students.Clear();
//add Toner items
foreach (var studentId in mathClassModel.SelectedStudents)
{
var student = db.Student.Find(int.Parse(studentId));
mathClassModel.Students.Add(student);
}
if (ModelState.IsValid)
{
db.SaveChanges();
return RedirectToAction("Index");
}
return View(mathClassModel);
}
In terms of entities (or objects) you have a Class
object which has a collection of Students
and a Student
object that has a collection of Classes
. Since your StudentClass
table only contains the Ids and no extra information, EF does not generate an entity for the joining table. That is the correct behaviour and that's what you expect.
Now, when doing inserts or updates, try to think in terms of objects. E.g. if you want to insert a class with two students, create the Class
object, the Student
objects, add the students to the class Students
collection add the Class
object to the context and call SaveChanges
:
using (var context = new YourContext())
{
var mathClass = new Class { Name = "Math" };
mathClass.Students.Add(new Student { Name = "Alice" });
mathClass.Students.Add(new Student { Name = "Bob" });
context.AddToClasses(mathClass);
context.SaveChanges();
}
This will create an entry in the Class
table, two entries in the Student
table and two entries in the StudentClass
table linking them together.
You basically do the same for updates. Just fetch the data, modify the graph by adding and removing objects from collections, call SaveChanges
. Check this similar question for details.
Edit:
According to your comment, you need to insert a new Class
and add two existing Students
to it:
using (var context = new YourContext())
{
var mathClass= new Class { Name = "Math" };
Student student1 = context.Students.FirstOrDefault(s => s.Name == "Alice");
Student student2 = context.Students.FirstOrDefault(s => s.Name == "Bob");
mathClass.Students.Add(student1);
mathClass.Students.Add(student2);
context.AddToClasses(mathClass);
context.SaveChanges();
}
Since both students are already in the database, they won't be inserted, but since they are now in the Students
collection of the Class
, two entries will be inserted into the StudentClass
table.