none
Entity Framework 4.3.1: Issue updating grandparent object when grandchild is part of the object graph RRS feed

  • Question

  • I’m having a bit of an issue saving a many-to-many relationship when the child object has its own collection of child objects. We are using EF 4.3.1 "Code First"

    Our object layout is analogous to the following: “Plan” has many “Tasks”. “Tasks” have many “States”. Tasks can be shared between Plans. States can be shared between Tasks. There is a pre-determined list of States in the system.

    In our MVC 3.0 application, we present a UI in which a user can choose from a list of Tasks to add to a Plan. We encounter the following exception when we try to update the collection of Tasks when there is more than one Task in the collection with the same State. The exception is “An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.”

    We are using the following pattern in our repositories to save objects that have child collections:

    public void Update(Plan plan)
    {
       
    var original =
             _context
    .Plans.Where(hp => hp.PlanId == plan.PlanId)
                 
    .Include(x => x.Tasks.Select(y => y.States));

       
    var planEntry = _context.Entry(original);
        planEntry
    .CurrentValues.SetValues(plan);

       
    //insert new task entries
       
    foreach (var newItem in plan.Tasks.Where(n => !original.Tasks.Contains(n)))
       
    {
            _context
    .Tasks.Attach(newItem);
            original
    .Tasks.Add(newItem);
       
    }

       
    //remove deleted omitted for brevity
    }

    The reason why we are grabbing the Plan with all Tasks and States is because we have validation on the Plan that looks for Tasks that have particular States.

    We get the following exception when trying to attach the new Task to the Plan: “An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.”

    The funny thing is that the “newItem” is not in the _context.Tasks.Local collection. It appears that the exception is thrown because the “newItem” shares a State with one of the existing Tasks. Since the State is already in the ObjectContext, we cannot add it again. I suppose we could loop over the new Task’s States and detach them before attaching the Task, but adding and removing the children actually happens in a generic helper class. The code above has been re-written for the sake of brevity/clarity.

    My questions are:

    1. Is this the best way to go about saving an object that has a child-collection?

    2. Why is the Entity Framework trying to attach the State when attaching the Task?

    3. Is there a way to query EF to have the child collection Detached (like AsNoTracking(), but just on the children?)

    Any other comments or insights would be greatly appreciated.

    PS.  This is an xpost from StackOverflow: 

    http://stackoverflow.com/questions/10840322/entity-framework-4-3-1-issue-updating-grandparent-object-when-grandchild-is-par

    • Edited by lafferrs Friday, June 8, 2012 1:28 PM
    Friday, June 8, 2012 1:28 PM

All replies

  • Hi lafferrs,

    Welcome to MSDN Forum.

    I'm not clear about "Tasks can be shared between Plans. States can be shared between Tasks", if the relationship is that, I think it is not a one to many relationship, it's a many to many relationship. Could you please clarify the scenario?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, June 11, 2012 6:31 AM
    Moderator
  • Hi Allen, 

    It is definitely a many-to-many relationship between Plans and Tasks.  I called "Plan" the parent and "Task" the child simply because that's how the business views the objects.

    Thanks for your reply.


    Monday, June 11, 2012 6:00 PM
  • Hi lafferrs,

    It will not caused by the shared states. Please check the 'plan' which you passed into the method, make sure whether this plan's tasks are detached.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, June 12, 2012 8:22 AM
    Moderator
  • Hi lafferrs,

    Any update about this issue? If you need further help, please feel free to let me know, I will be more than happy to be of assistance.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, June 18, 2012 2:40 AM
    Moderator