none
STE: relations fix-up after FK update RRS feed

  • Question

  • 1) There are three entities A, B, C. A has many to one relation to B and C (A.BId / A.B and A.CId / A.C).

    2) A being loaded somewhere in the code and one or both foreign keys (A.BId and/or A.CId) are updated with a new value. ApplyChanges being called.

    3) B and/or C matching new values being loaded using the same context in another part of code.

    Result: A.B == null, A.C == null

    I know it is a normal behaviour of STE to set relationships to null when foreign keys are updated, but why references are not fixed even when ApplyChanges was already called and new related objects were loaded? Is there some workaround to get full graph automatically?

    Thanks in advance.

    Thursday, July 26, 2012 7:15 PM

Answers

All replies

  • Hi AlikDev,

    Welcome to the MSDN forum.

    Based on this document: http://msdn.microsoft.com/en-us/library/ff407090.aspx

    When you change the relationship between objects by setting the foreign key property, the reference navigation property is set to null and not synchronized to the appropriate principal entity on the client. After the graph is attached to the object context (for example, after you call the ApplyChanges method), the foreign key properties and navigation properties are synchronized.

    Since Self-tracking entities are not enabled to perform lazy loading, I recommend you check if related object is loaded. For example:

        var contact =
            (from c in context.Contacts
             where c.ContactID == contactID
             select c).First();
    
        // Load the orders for the customer explicitly.
        if (!contact.SalesOrderHeaders.IsLoaded)
        {
            contact.SalesOrderHeaders.Load();
        }

    Good day!


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Friday, July 27, 2012 7:35 AM
  • Hi AlikDev,

    Welcome to the MSDN forum.

    Based on this document: http://msdn.microsoft.com/en-us/library/ff407090.aspx

    When you change the relationship between objects by setting the foreign key property, the reference navigation property is set to null and not synchronized to the appropriate principal entity on the client. After the graph is attached to the object context (for example, after you call the ApplyChanges method), the foreign key properties and navigation properties are synchronized.

    Since Self-tracking entities are not enabled to perform lazy loading, I recommend you check if related object is loaded. For example:

        var contact =
            (from c in context.Contacts
             where c.ContactID == contactID
             select c).First();
    
        // Load the orders for the customer explicitly.
        if (!contact.SalesOrderHeaders.IsLoaded)
        {
            contact.SalesOrderHeaders.Load();
        }

    Good day!


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Friday, July 27, 2012 7:35 AM
  • After the graph is attached to the object context (for example, after you call the ApplyChanges method), the foreign key properties and navigation properties are synchronized.

    It is exactly a problem I have - they are not synchronized in my case.

    Also, I found that related entities are loaded before ApplyChanges call, point 3 is just a GetObjectByKey call retrieving data from memory, not database as I thinked initially.

    Friday, July 27, 2012 1:01 PM
  • Thanks to document Alexander mentioned in his post and debugger I found a reason.

    Self tracking entites were not designed to be retrieved and submitted in the same context, there is just no code to handle it using standard T4 templates. So STE must be detached from context before ApplyChanges call.

    Friday, July 27, 2012 2:47 PM
    • Marked as answer by Alexander Sun Monday, July 30, 2012 9:26 AM
    Friday, July 27, 2012 4:46 PM