這一直使我瘋狂了一個星期。無法刪除具有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.
我只是不明白這一點。任何人都知道我可能做錯了什麼?我一直在谷歌的一天,但無濟於事
你確定這是它的原因嗎?的ComplexType? – Meligy 2010-11-18 04:22:29
有點奇怪,因爲我猜我現在有同樣的問題。該實體對於枚舉也具有複雜類型屬性。不想快速跳到結論 – Meligy 2010-11-18 04:23:22
@Mohammed:在我的例子中,複雜類型的'VoteTypeWrapper'是錯誤信息中提到的屬性(見上),所以我認爲對我來說這是安全的複雜的類型是原因。另外,只要我刪除它一切正常。如果它看起來像一隻鴨子,聽起來像鴨子......;) – 2010-11-19 14:45:26