This is a migrated thread and some comments may be shown as answers.

Help on updatine detail in master detail relationship

2 Answers 37 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
kieron
Top achievements
Rank 1
kieron asked on 11 Oct 2011, 02:15 AM
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.

2 Answers, 1 is accepted

Sort by
0
Ivailo
Telerik team
answered on 13 Oct 2011, 10:22 AM
Hello Kieron,

There is a property of the Association between the PEHorganisermaster and PEHcontract Domain Classes called IsManaged that can help you achieve what you what. When this property is set to True, the relationship between the navigation property newOrganiser.PEHcontacts and the actual contract objects is automatically maintained, meaning that when you add a new contract the Organiser_id property will be populated.

By default, the IsManaged property is set to False for all your associations for performance reasons, so you have to set it to True when you need this linkage automated for you.

Do not hesitate to let us know if you need any further clarifications on this.

Greetings,
Ivailo
the Telerik team

Check out the latest stable build of Telerik OpenAccess ORM. Download it and benefit from our new Project Templates.

0
kieron
Top achievements
Rank 1
answered on 18 Oct 2011, 02:05 AM
Thanks by enabling that it worked like a treat
Tags
Getting Started
Asked by
kieron
Top achievements
Rank 1
Answers by
Ivailo
Telerik team
kieron
Top achievements
Rank 1
Share this question
or