You create the relationship between mentee
and address
by adding the address
to the mentee.addresses
collection (or the other way around, it doesn't matter), like this:
if (ModelState.IsValid)
{
menteeViewModel.mentee.addresses.Add(menteeViewModel.address);
db.mentees.Add(menteeViewModel.mentee);
db.SaveChanges();
}
You don't need to add the address
to the context explicitly in this example because it will happen automatically together with adding the mentee
. The code will INSERT a new mentee
to the database, a new address
and a link record into the link table Mentee_Address
. I'm not sure if you want to insert both mentee
and address
, but that is what your original code would do as well. My example above just adds the link record to the procedure.