For example i have a Contact table that is related to Addresses. To communicate with database i use WCF Data Services. I have a form, where there are contact information with list of possible addresses on it. For now i create list of ‘Addresses to insert’ and insert Contact using (context is entity data context here):
context.AddToContact(contact);
context.SaveChanges();
After that i can get the inserted contact.ID and add it as a parent id to all addresses in List of Addresses:
cacheAddressList.ForEach(a =>
{
address.ContactID = contact.ID;
context.AddToAddress(address);
}
);
context.SaveChanges();
So i have to do 2 inserts.
I know that in Entity Framework i can add children to parent if i have navigation properties between them. In my case i DO have navigations, but such code doesnt work (contact hasnt been saved yet..):
context.AddToContact(contact);
cacheAddressList.ForEach(a =>
{
address.Contact = contact;
}
);
context.SaveChanges();
Is it possible to insert all children and parents in one transaction here? Because if it is possible-i do not have to create all that lists of children to add after parent insert..
Here’s the code sample to do this:
Hope this helps.
Thanks
Pratik