OrganizationServiceContext.SaveChanges() is not always updating the new ID values on new RelatedObjects

Unanswered OrganizationServiceContext.SaveChanges() is not always updating the new ID values on new RelatedObjects

  • Friday, May 04, 2012 6:54 PM
     
      Has Code

    I have some LateBound code that will create new custom entities in our CRM system.

    These new entities also occassionally have new N-N related entities.

    My logic is as follows:

    newEntityA = new Entity("CustomA");
    orgCtxt.AddObject(newEntityA);
    newEntityB = new Entity("CustomB");
    newRelationship = new Relationship("customA_customB");
    orgCtxt.AddRelatedObject(newEntityA, newRelationship, newEntityB);
    
    orgCtxt.UpdateObject(newEntityA);  // I do this for updates and creates (not sure if it matters)
    
    orgCtxt.SaveChanges();

    And then when I try to access my newEntityA, it always has a new Guid assigned to the .Id property.  However, when I access the .Id property on the new related object (newEntityB), occassionally it is updated, but often it remains at a Guid.Empty value.

    If I set the related object's Guid to Guid.NewGuid() after I create it and before I Save it, then it creates the new object with the new Guid just fine.  That always works.

    I am just wondering why SaveChanges() will randomly update that .Id value on my objects.  Is SaveChanges() sometimes an asynchronous operation on the server side, and other times a synchronous one?  And also, is it a bad thing to set my own new Guid values on new entities in code before saving them?

All Replies

  • Saturday, May 05, 2012 5:15 AM
     
     

    HI Chris,

                    I don’t thing is a bad thing to set your own new Guid if that works for you. The guid algorithm is configured so you should always get a new and “unique” value regardless of what computer or country you call the method. So that means you would never get a duplicate value. 


    Regards,
    Damian Sinay