I’m trying to save an object back to the database via WCF, like so (pseudo code):
var Contact = new Contact
{
Id = Guid.NewGuid(),
Name = "Test",
Address = new Address
{
Id = Guid.NewGuid(),
Postcode = "blah"
}
};
In my model the relationship is 1 to 1 with Contact and Address but doing this fails:
DB.AddToContacts(Contact);
DB.SaveChanges();
Complaining the relationship isn’t met and can’t be empty. So I tried this approach:
DB.AddToContacts(Contact);
DB.AddToAddresses(Contact.Address);
DB.AddRelatedObject(Contact, "Address", Contact.Address);
DB.SaveChanges();
but now I get “AddRelatedObject method only works when the sourceProperty is a collection.”.
My question: how do I save back a related entity easily via WCF?
I finally got it to work by doing this:
That is 4 individual calls over the wire to the WCF service to add a relational object. At the end of it all I’ll be making 14 calls, eek!