Adding to a new Entity in ObjectStateManagerChanged event RRS feed

  • Question

  • Scenario:

    I'm handling the ObjectStateManagerChanged event.

    In this event, I detect if the item changing for the event is a CollectionChangeAction.Add.

    I cast the element to the actual entity in question, and assign it a new primary key (Guid)

    Once that is done, I add an arbitrary number of child items to the item that was just added.  I also ensure that the foreign key in the child item being added points to the entity being added.

    E.g. itemAdded.ChildData.AddObject(...)

    The loop that adds the child data runs fine, but somewhere else in the application's execution, and object reference error is raised.  I can't trap it with the debugger.

    Is this is a bug?  Is there a better way to add to an entity when that entity itself is being created?

    Tuesday, July 12, 2011 11:34 PM

All replies

  • Hi,

    Why do you want to set a new Guid on the entity while you add the entity to the StateManager?

    CollectionChangeAction.Add does not reflect the state of the entity, only the operation that happens on the ObjectStateManager collection. No matter if you call AddObject or Attach the action will be CollectionChangeAction.Add, if you detach the CollectionChangeAction.Remove will be used. The ObjectStateManagerChanged event would also be called whenever you fetch or refresh data from the store.

    So, in your code you still have to check the state of the entity by getting the ObjectStateEntry, and based on this decide if you need to set a new Guid or not.

    Hope this explains it!

    Wednesday, July 13, 2011 7:35 AM
  • Thanks for the reply.

    To clarify further,

    I'm assigning a new Guid primary key to the entity if it is in the 'Added' state, and the primary key's guid value is Guid.Empty. This routine is just to assign a new PK for new data.

    Within this same routine, I am adding child data to this new entity.  Some time immediately after the ObjectStateManagerChanged event, an object reference error occurs.

    Shall I just post a sample project?

    Wednesday, July 13, 2011 4:04 PM
  • Yes, that could help :)
    Wednesday, July 13, 2011 6:36 PM
  • Hi Ajhu,


    Welcome to the forum.

    I had test that we can change the entity in the ObjectStateManagerChanged method. Could you show us the error message

    and it's bettter to post a sample.



    David Peng [MSFT]
    MSDN Community Support | Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 14, 2011 8:28 AM
  • I think get the same error. When adding a new entity to the context by use of "System.Data.Objects.ObjectContext.AddObject(..)" I want to execute some BusinessLogic in order to create other entities logically linked (usually with 1:1 relationship). For this I am using the ObjectStateManagerChanged event. But after this event is executed a object reference error is raised.

    If I skip the code that adds and links the entities (in the ObjectStateManagerChanged event) everything executes correctly. It seems to be a problem to add other EntityObjects to the ObjectContext during the ObjectStateManagerChanged event. The strange thing is that my code is working fine in .net 3.5, but gives a NullReferenceException in 4.0.

    I've tried to debug the .net framework, and figured out that it crashes in the internal ObjectContext.AddSingleObject method after it (indirectly) invokes the ObjectStateManagerChanged event. This exception occures because the internal ProcessedEntities hashset is made null at the end of the public AddObject method. But if the EntityObject is created during the ObjectStateManagerChanged event this hashset should not be made null.

    It seems to be a framework bug.

    • Edited by EdwinMSDN Thursday, September 8, 2011 8:48 AM Found out why it crashes
    Tuesday, September 6, 2011 12:45 PM