views:

36

answers:

1

I have 2 tables : Item and Location (one - many ).

I select one location and I try to update it

The entity goes from {Id=2, Name="name1",City="city1",Items=null} to {Id=2, Name="name1", City="city2",Items=null} and i want to save the updates. The update method from the base class is:

        public virtual void Update(T entity)
        {
            Entities.Attach(entity);
            Context.ObjectStateManager
                   .ChangeObjectState(entity, System.Data.EntityState.Modified);
        }

I make several updates just like this on other tables with no problems but in this case I get "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." and I really don't understand why.

+2  A: 

You can try specifying: MergeOptions.NoTracking for the context and see if that solves your problem.

Also, you can try Context.ObjectStateManager.TryGetObjectStateEntry(entity, out stateEntry) to see if the object is there and can be updated.

Michael Goldshteyn
stateEntry=null, it seems the object is not there and it shouldn't be, i just can't figure out why i cannot attach it
gigi
MergeOption.NoTracking did the job. In which situations must that be set when using POCO?
gigi
MSDN describes the relationship between NoTracking and POCO: http://msdn.microsoft.com/en-us/library/system.data.objects.mergeoption.aspx although its very terse on the subject.
Michael Goldshteyn