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 t开发者_运维百科heir 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??
Three choices:
- The edit view can re-select the workstation from its own context based on the PK of the entity from the other view.
- You can
Detach
the workstation from the list view and thenAttach
it to the edit view. - 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.
精彩评论