This question is locked. New answers and comments are not allowed.
I am having some issues finding documentation on adding a new child in a 1to many relationship.
I have a class organiser which has many contacts. use winforms i have done the below
PEHorganisermaster newOrganiser = new PEHorganisermaster();
newOrganiser.Name = NameTextEdit.EditValue.ToString();
newOrganiser.Address1 = Address1TextEdit.EditValue.ToString();
newOrganiser.Address2 = Address2TextEdit.EditValue.ToString();
newOrganiser.Suburb = SuburbTextEdit.EditValue.ToString();
newOrganiser.State = StateTextEdit.EditValue.ToString();
newOrganiser.Postcode = Convert.ToInt32(PostcodeTextEdit.EditValue.ToString());
newOrganiser.Country = countrytextedit.editvalue.tostring();
newOrganiser.Active = 1;
dbcontext.Add(newOrganiser);
dbcontext.SaveChanges();
Now i wish to start adding contacts to this organiser which are in a seperate table but i am unsure on how to do it so that the new contact is related to this organiser?
I have tried below
PEHcontact newcontact = new PEHcontact();
newcontact.Name = gridView1.GetRowCellValue(e.RowHandle, colName).ToString();
newcontact.Active = 1;
newcontact.Mobile = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, colMobile).ToString());
newOrganiser.PEHcontacts.Add(newcontact);
dbcontext.SaveChanges();
And it does indeed add the contact to teh contact table the the Organiser_id field is blank which is the foreign key. i would have that that the ORM would auto add the ID from the neworganiser instance.
I have a class organiser which has many contacts. use winforms i have done the below
PEHorganisermaster newOrganiser = new PEHorganisermaster();
newOrganiser.Name = NameTextEdit.EditValue.ToString();
newOrganiser.Address1 = Address1TextEdit.EditValue.ToString();
newOrganiser.Address2 = Address2TextEdit.EditValue.ToString();
newOrganiser.Suburb = SuburbTextEdit.EditValue.ToString();
newOrganiser.State = StateTextEdit.EditValue.ToString();
newOrganiser.Postcode = Convert.ToInt32(PostcodeTextEdit.EditValue.ToString());
newOrganiser.Country = countrytextedit.editvalue.tostring();
newOrganiser.Active = 1;
dbcontext.Add(newOrganiser);
dbcontext.SaveChanges();
Now i wish to start adding contacts to this organiser which are in a seperate table but i am unsure on how to do it so that the new contact is related to this organiser?
I have tried below
PEHcontact newcontact = new PEHcontact();
newcontact.Name = gridView1.GetRowCellValue(e.RowHandle, colName).ToString();
newcontact.Active = 1;
newcontact.Mobile = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, colMobile).ToString());
newOrganiser.PEHcontacts.Add(newcontact);
dbcontext.SaveChanges();
And it does indeed add the contact to teh contact table the the Organiser_id field is blank which is the foreign key. i would have that that the ORM would auto add the ID from the neworganiser instance.