locked
How to save an object and its related objects RRS feed

  • Question

  • Hi I am attempting to save an entity object with its related objects but only the top level entity object gets saved. I have a contact object and a related address object. Here is my code:

    Public Function UpdateContact(ByVal UpdatedContact As Contacts) As Contacts
            Using TalentContext = New TalentManagerDBEntities
                Dim key = TalentContext.CreateEntityKey("Contacts", UpdatedContact)
                Dim OriginalContact As Contacts = Nothing
                If TalentContext.TryGetObjectByKey(key, OriginalContact) Then
                    TalentContext.ApplyPropertyChanges(key.EntitySetName, UpdatedContact)
                    TalentContext.SaveChanges()
                    TalentContext.Refresh(Data.Objects.RefreshMode.StoreWins, UpdatedContact)
                 End If
                Return UpdatedContact
            End Using
     End Function

    The updatedContact contains the updated values for the contact and contact.address but only changes made to the contact objects are saved in the database.

    Please help.

    Friday, March 9, 2012 7:07 PM

All replies

  • Hi,

    you have to attach the related objects to the context, not just the parent one.

    http://msdn.microsoft.com/en-us/library/bb896245.aspx#Y0 

    thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by Allen_MSDN Tuesday, March 13, 2012 2:33 AM
    • Unmarked as answer by Allen_MSDN Thursday, March 15, 2012 1:53 AM
    Friday, March 9, 2012 8:50 PM
  • Hi Julia,

    Thanks for the response. I forgot to mention that there is a pure join table(many to many relationship), ContactAddresses, that relates the contacts to the addresses. I was able to add the detached objects to the context but i get following error:

    Unable to update the EntitySet 'ContactAddresses' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation. 

    Thanks.

    Wednesday, March 14, 2012 2:38 AM
  • I am not sure why you would get this error.

    Could you please email the project to me at juliako@microsoft.com, so I can take a look?

    thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.

    Wednesday, March 14, 2012 5:34 AM
  • Hi askarii,

    Could you please send the project to Julia or to v-alll@microsoft.com, we will help you to test the code and to find the root cause.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 15, 2012 1:57 AM