David
David

Reputation: 3609

Entity Framework 4 error: An entity object cannot be referenced by multiple instances of IEntityChangeTracker

I am using EF4 for the first time and have adopted a strategy of a UnitofWork (DataContext) per View. However, I am having issues and seek advice.

I have a window that displays a list of workstations, when I click the edit button I have another window that displays the selected workstation for edit.

The list view and the edit view use their own UnitOfWork, the selected workstation is passed through to the edit view, however when I try to save the workstation on the edit view I get the following;

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

I know that this is because the workstation object that I passed through to the edit view has a data context associated with it.

How should i deal with this??

Upvotes: 0

Views: 813

Answers (1)

Craig Stuntz
Craig Stuntz

Reputation: 126547

Three choices:

  1. The edit view can re-select the workstation from its own context based on the PK of the entity from the other view.
  2. You can Detach the workstation from the list view and then Attach it to the edit view.
  3. If the list view is read only, you can use MergeOption.NoTracking to prevent the context from tracking changes at all. You still would need to attach it to the edit context.

Upvotes: 1

Related Questions