[廚餘回收] Entity Framework Core 錯誤:「...cannot be tracked because another instance...」的原因與解法

從應用程式日誌看到下面這個 Entity Framework Core(以下簡稱 EF Core)發出的例外錯誤:

The instance of entity type 'MyTable' cannot be tracked because another instance with the same key value for {'Key'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.

它的意思是,相同主索引鍵的實體無法被追蹤,因為已經有另一個實體正在被追蹤,我們來看看怎麼回事。

...繼續閱讀 »