Updating Entity PK/FK

Posts   
 
    
RMarietta
User
Posts: 5
Joined: 23-May-2012
# Posted on: 21-Aug-2012 21:20:43   

First of all, I'm using GenPro v3.5 with the adapter model and gen pro runtime framework.

A little background:

In my application I'm integrating with another (3rd Party) application using entities that are populated by reading from the 3rd party database but the Adds/Updates must occur via external web services. So I can use FetchEntityCollection(), prefetches, filters and all that good stuff to get the data/entites I need but when modifying data I need to use the updated values on the entity to generate a web service request to perform the change.

The web services have a 1:1 mapping to the entites. So if I had a contact entity with child phone entites then I would start off by mapping the contact (parent) entity (only) to an "AddContact" web service request. I would get back an ID for the new contact and then proceed to call the "AddContactPhone" method for the child phone entites. Because I'm not using the entity to save, the entity fields remain Dirty and without PK/FK values. I'm manually updating the IsDirty/IsChanges flag within the Entity graph to indicate what has been saved or not.

Here is the question: After adding the contact I want to update the Contact.ID field value (PK) and have that propogate down to all child entites. So that when I pass the ContactPhone child entity to the "AddContactPhone" WebService method it has the contact ID that was just added.

If I set the contact id value, I lose the child entites (dereferenced). The documentation states "Changing PK fields is not recommended and changed PK fields are not propagated to related entities fetched in memory"... but in my case I'd like to.

Is it possible to make this happen (update all FKs withinthe graph when a PK is updated on the parent entity)? Or if I need to iterate through the graph and update all child entity FKs, how would I do that?

Thanks, Ryan

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Aug-2012 22:44:40   

You will need to iterate on them. And I suggest passing the FK value to the service call, and have the iteration code run at the service.

RMarietta
User
Posts: 5
Joined: 23-May-2012
# Posted on: 23-Aug-2012 21:12:46   

Thanks, I'd like to keep the values in the entity so we can continue to just pass the entity around.

I found that I can still keep the child entities if I do the following: 1. Iterate through the child entities 2. Define a variable that has a reference to the child entity 3. Update the FKs (the child entity is removed from the relationship collection, but I still have a reference to it) 4. Add the entity to a separate EntityCollection<> 5. At the child entites back to the parent entity (Contact.Phones.AddRange)

Example:


[TestMethod]
public void EntityUpdatePkTest()
{
    var contact = GetMockContact();
    contact.Id = "1234"; // update the pk

    // update the fk for all child phone entities
    var phoneCountBefore = contact.Phones.Count;
    var phones = new EntityCollection<ContactPhoneEntity>();
    while (contact.Phones.Count > 0)
    {
        var phone = contact.Phones[0];
        phone.ContactId = contact.Id;
        phones.Add(phone);
    }
            
    contact.Phones.AddRange(phones);
    Assert.AreEqual(phoneCountBefore, contact.Phones.Count);
}

Now the child entities are back with the correct Fks. I assume because I'm adding the reference back to the collection that I'm not adding another entity...