开发者

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:

  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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜