簡體   English   中英

意外的“此EntitySet中已經存在具有相同標識的實體”?

[英]Unexpected “An entity with the same identity already exists in this EntitySet”?

嘗試添加到客戶端EntitySet時出現錯誤。 下面的代碼在Add方法上間歇性地失敗。

// On the DB side this has a primary key field of IDENTITY(1,1) called Id
var map = new EpisodeDictionaryMap();
map.DictionaryName = dictionary; // String
map.Section = section; // String
map.DictionaryVersion = version; // Int32

episode.EpisodeDictionaryMap.Add(map);

我還不是在每次調用此代碼之前,而是在用戶一次切換到應用程序內的其他屏幕時,都在EpisodeEpisodeDictionaryMap實體Episode (以及其他Clear()上調用Clear() 這是最近的更改,因此可能是問題的一部分。

研究DomainContext的EpisodeDictionaryMap集合后,似乎有時實際上存在多個“ New” EpisodeDictionaryMap實例,每個實例的Id為0 ,正如我在保存之前所期望的那樣,在這些實例中,保存完成沒有問題,並且正確將ID分配給客戶端實體。

鑒於在任何時候(至少在它們處於“新”狀態時)在EntitySet中具有多個ID為0條目似乎都不是問題,所以我不理解該錯誤是什么消息試圖告訴我。

我在一些相關的SO問題上注意到,組合可能是個問題,但是我沒有使用它。 除了情節引用本身之外,也沒有其他外鍵引用,這有時也會導致此錯誤。

建議,想法? 非常感謝!

編輯

完全異常(不存在內部異常)

System.InvalidOperationException occurred
    Message=An entity with the same identity already exists in this EntitySet.
StackTrace:
    at System.ServiceModel.DomainServices.Client.EntitySet.AddInternal(Entity entity)
    at System.ServiceModel.DomainServices.Client.EntitySet.<Add>b__5(EntitySet l, Entity e)
    at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.Visit(Entity entity)
    at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.VisitEntityCollection(IEntityCollection entityCollection, PropertyInfo propertyInfo)
    at System.ServiceModel.DomainServices.Client.EntityVisitor.Visit(Entity entity)
    at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.Visit(Entity entity)
    at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.VisitEntityRef(IEntityRef entityRef, Entity parent, PropertyInfo propertyInfo)
    at System.ServiceModel.DomainServices.Client.EntityVisitor.Visit(Entity entity)
    at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.Visit(Entity entity)
    at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.VisitEntityRef(IEntityRef entityRef, Entity parent, PropertyInfo propertyInfo)
    at System.ServiceModel.DomainServices.Client.EntityVisitor.Visit(Entity entity)
    at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.Visit(Entity entity)
    at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.Infer(EntityContainer container, Entity entity, Action`2 action)
    at System.ServiceModel.DomainServices.Client.EntitySet.Add(Entity entity)
    at System.ServiceModel.DomainServices.Client.EntityCollection`1.Add(TEntity entity)
    at TangoSoft.Client.EpisodeExtensions.SaveDictionaryVersion(Episode episode, String dictionary, String section, Int32 version)

驗證有關表的主鍵(在xml中拉出.edmx文件)在兩個部分中都具有StoreGeneratedPattern =“ Identity”。 存在一個已知的錯誤,其中通過設計器的EF修改了一個部分,而沒有修改另一部分。 在這兩個部分中都需要查找:

<EntityType Name="TIMELINE">
          <Key>
            <PropertyRef Name="ID"/>
          </Key>

  <Property Name="ID" Type="decimal" Nullable="false" Precision="19" StoreGeneratedPattern="Identity" />

請參閱EF4:StoreGeneratedPattern SSDL中的錯誤

經過大量的試驗和錯誤,我發現僅間接連接到錯誤實體的實體仍在上下文中徘徊,並且將ID為0的實體保留在周圍。 我一直在清除包含非查找數據的每個EntitySet-事實證明這不夠。 在VS中進行調查並不能為我提供一種簡單的方法來實際找到所涉及的實體,但是通過對ANTS進行分析后發現,有太多事情只與我的“查找” EntitySet實體有關。

我的“解決方案”現在是清除所有我的EntitySet,如下所示:

foreach (var set in _context.EntityContainer.EntitySets)
{
    set.Clear();
}

令我困惑的是,這是一個間歇性錯誤。 有時,上下文為T且ID為0多個實體並不重要,而有時它會出錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM