Data Platform Developer Center > Data Platform Development Forums > ADO.NET Entity Framework and LINQ to Entities > Entity with referenced child entities throws exception on save
Ask a questionAsk a question
 

QuestionEntity with referenced child entities throws exception on save

  • Wednesday, October 14, 2009 1:22 PMnologin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    When I create a new entity (with already existing referenced child entities) and try to save it, I get the well known exception of 'An object with the same key already exists in the ObjectStateManager '.
    I already tried to use the EntityHelper class where in the AddObject method the relations are removed, the new entity is added and then the relations are recreated. The child objects have 'many to many' associations to the parent entity. Also tried to check with TryGetObjectStateEntry if the main entity exists but it does not.
    How can I check the referenced entities? Or how can I tell whether they cause the problem?
     
    Andris

All Replies

  • Sunday, October 18, 2009 8:52 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Andris,

    Does the entity you want to insert exists in DB?
    Is the entity's state is detached?

    Could you give more description about the steps for reproducing the error?


    Thanks!
    Yichun Feng
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Monday, October 19, 2009 10:09 AMnologin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Which entity do you refer to by that question? The new entity obviously does not exist in the database, the referenced child entities exist in the database (as I could use the insert method once, they were used in that).

    I tried to get that state of the new entity and I got the following exception:

    context.ObjectStateManager.GetObjectStateEntry(entity).State.ToString()
    
    (translated):
    The ObjectStateManager does not contain any ObjectStateEntry that has the type of 'MainEntities.EntityA'.

    Or should I check the referenced entites? Please provid some guidance for getting the proper information. Thanks.
    Andris
  • Tuesday, October 20, 2009 2:33 PMnologin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I forgot to mention that the entites that are supposed to be saved, are send through a WCF service. My colleague adviced me to check the ObjectStateManager on both sides (client and service) and see if the entities are presented in the manager. Any ideas regarding to this?
    Andris
  • Wednesday, October 21, 2009 12:23 PMnologin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Well, the case is getting more interesting. I found where the exception is thrown.

    In the EntityHelper class the relations have been removed, the new entity with detached state is added and when the relations are about to be recreated, in the following section throws the exception:

    foreach (RelationsShipMapping mapping in rs)
            {
    
              // check if we need to attach the related entity object
    
              IEntityWithKey entityWithKey = mapping.TargetEntity as IEntityWithKey;
    
              if (entityWithKey != null && entityWithKey.EntityKey != null)
    
                context.Attach(entityWithKey);
            }
    
    The entityWithKey object's EntityState is Detached (this entity exists in the database, it is a child table to the new main entity).
    If I use context.ObjectStateManager.GetObjectStateEntry(entityWithKey), it throws an exception of that there is no entity in the manager that has the type of the entity.
    If then the context.Attach(entityWithKey) is called, I get the exception of 'An object with the same key already exists in the ObjectStateManager '.
    I sense a contradiction here (probably because of my lack of necessary knowledge):
    An entity, that is detached and not in the manager, cannot be added because it is in the manager.
    Please, I need some clarification...

    Andris
  • Wednesday, October 21, 2009 2:04 PMbjkj Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Does the child entity you are attaching have child entities or relations?  The entity your are attaching may not exist in the ObjectStateManager, but it's related items might already.  Like the navigation property from the child back to the parent.
  • Sunday, October 25, 2009 6:52 PMnologin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The child entity obviously have relations as in the initial post I bolded out: the child entities (are supposed to) belong to many parent entities (many-many relationship).

    The new parent entity is in the manager with the state of Detached.
    The child entity is in the manager with the state of Detached.
    When attaching the new parent entity, it works.
    When trying to attach the detached and already existing child, it fails. Obviously the connection table should take a newly inserted record. It seems that the 'many' attribute is ignored as it seems that it tries to connect the parent to the previously inserted entity (?). I really don't know why a simple insert takes so much suffering...



    Andris
  • Tuesday, October 27, 2009 6:56 PMnologin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I guess I can circumscribe what is causing the problem:

    I create the main entity like this: Entity a=new EntityA()
    I set the its child to an existing entity like this: a.ChildEntity=b;

    After this, both entity are Detached state.
    And here comes the problem: when I call context.AddToEntityA(a), it tries to add the existing b entity again (even if I set its EntityState property to Unchanged by attaching it), no matter what, obviously causing an exception. Can you tell me why this happens?
    Andris
  • Thursday, November 05, 2009 2:25 PMnologin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    As there was no rational explanation about why the EF 1.0 doesn't work in several cases, the leaders of our company decided to drop the using of EF 1.0 and consider it as not market-ready. Because of this the DAL tier of our current project needs to be reshaped with Enterprise Library 4.1. Too bad, it looked promising. I hope, EF 2.0 will provide much more reliable solutions.
    R.I.P. EF 1.0...
    Andris
  • Thursday, November 05, 2009 4:29 PMNoam Ben-Ami - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sorry you did not have a good experience with EF3.5. The solution for that release of the EF is to use Danny Simmons' "EntityBag" to help track changes across tiers. For EF4.0, we have a number of n-tier improvements, including Self-Tracking Entities, which carry change tracking information across tiers.

    See here:
    http://blogs.msdn.com/adonet/archive/2009/11/04/ado-net-entity-framework-community-technology-preview-released.aspx


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