2010-06-22 35 views
1

這一直使我瘋狂了一個星期。無法刪除具有ComplexType屬性的實體(實體框架4)

我有一個類,看起來像這樣:

public class SuggestionVote 
{ 
    public virtual int ID { get; set; } 
    public virtual Suggestion Suggestion { get; set; } 
    public virtual User User { get; set; } 
    public virtual VoteTypeWrapper VoteType { get; set; } 
    public virtual DateTime DateVoted { get; set; } 

    // Equality overrides omitted 
} 

VoteTypeWrapper實際上是基於文章on how to fake enums in Entity Framework 4枚舉的包裝,看起來像這樣:

public class VoteTypeWrapper 
{ 
     private VoteType _type; 

     public int Value 
     { 
      get { return (int)_type; } 
      set { _type = (VoteType)value; } 
     } 

     public VoteType EnumValue 
     { 
      get { return _type; } 
      set { _type = value; } 
     } 

     public static implicit operator VoteTypeWrapper(VoteType voteType) 
     { 
      return new VoteTypeWrapper { EnumValue = voteType }; 
     } 

     public static implicit operator VoteType(VoteTypeWrapper voteTypeWrapper) 
     { 
      return voteTypeWrapper == null ? VoteType.NotVoted : voteTypeWrapper.EnumValue; 
     } 
} 

VoteType枚舉之中:

public enum VoteType 
{ 
    Up, 
    Down, 
    NotVoted 
} 

我也defi斯內德一ComplexType模型設計師

<ComplexType Name="VoteTypeWrapper" > 
    <Property Type="Int32" Name="Value" Nullable="false" /> 
</ComplexType> 

我執行的作品有點像StackOverflow上的投票系統投票系統:用戶可以投票向上或向下;第二次投票撤消了先前的投票,並且在相反的方向投票(即,先前投票時下降)也撤消了投票。

現在的問題。投票一次就像一個魅力,所有的價值都正確地保存到數據庫。但是,撤消表決卻拒絕工作。要撤消投票,我基本上將投票標記爲撤消刪除,然後在上下文中調用SaveChanges。 只要我做的InvalidOperationException發生給我下面的消息:

The entity of type 'System.Data.Entity.DynamicProxies.SuggestionVote_4A3949F5B95E9A51567509467230FD7CEA0FB7761C3AC9C8C2BBC62BCAA033AF' 
references the same complex object of type 'Web.Model.VoteTypeWrapper' more than once. 
Complex objects cannot be referenced multiple times by the same entity. 

我只是不明白這一點。任何人都知道我可能做錯了什麼?我一直在谷歌的一天,但無濟於事

+0

你確定這是它的原因嗎?的ComplexType? – Meligy 2010-11-18 04:22:29

+0

有點奇怪,因爲我猜我現在有同樣的問題。該實體對於枚舉也具有複雜類型屬性。不想快速跳到結論 – Meligy 2010-11-18 04:23:22

+0

@Mohammed:在我的例子中,複雜類型的'VoteTypeWrapper'是錯誤信息中提到的屬性(見上),所以我認爲對我來說這是安全的複雜的類型是原因。另外,只要我刪除它一切正常。如果它看起來像一隻鴨子,聽起來像鴨子......;) – 2010-11-19 14:45:26

回答

0

嗯,我終於決定解決它,但只是映射int財產,而不是ComplexType。我還添加了一個(非映射)輔助屬性,以避免必須從intVoteType不斷投射。

我仍然喜歡爲我的問題得到答案,所以如果你能幫助它,我將不勝感激。在我將自己的答案標記爲正確之前,我會給它幾天的時間。

編輯:由於我沒有得到任何答案,所以我把自己的答案標記爲好。

0

我剛剛遇到同樣的問題。我有一個包含複雜類型的類,其中包含另一個複雜類型。我們稱他們爲Class1,Complex1和ChildComplex。我的情況如下(我不知道它是否與你的匹配,但錯誤信息完全相同)。

我從上下文中檢索Class1的實例並對Complex1的屬性進行更改。然後我打電話調用SaveChanges上下文,並得到同樣的異常:

The entity of type '<Class1>' references the same complex object of type '<Complex2>' more than once. 
Complex objects cannot be referenced multiple times by the same entity. 

我還沒有發現比克隆Complex1例如,在Class1的實例替換的克隆版本,然後再進行更改其他像樣的解決方法。這樣EF不會抱怨它是同一個複雜的對象。

這是一個非常奇怪的行爲。如果我有時間(大約2038年),我會嘗試隔離它並將其報告給MS,味道像臭蟲......

0

我剛剛一直在尋找這個相同的問題,但我剛剛意識到我誤解了錯誤。它並不抱怨你的實體有兩個相同類型的屬性,它抱怨說你在這兩個屬性中存儲了這個類型的「確切」相同的對象。

在我的情況下,我正在一張桌子上查找以獲得我的複雜類型,現在我意識到如果查找返回相同的值,它將是同一個對象。

我通過創建我的複雜類型的新對象並將其值設置爲與查找相同來解決此問題。