这一直使我疯狂了一个星期。无法删除具有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