views:

53

answers:

1

Hi everyone,

This has been driving me nuts for a week now.

I have a class that looks like this:

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 is actually an enum wrapper based on an article on how to fake enums in Entity Framework 4 and looks like this:

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;
        }
}

with the VoteType enumeration being:

public enum VoteType
{
   Up,
   Down,
   NotVoted
}

I've also defined a ComplexType in the model designer:

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

The voting system I'm implementing works somewhat like StackOverflow's voting system: The user can vote up or down; voting a second time undoes the previous vote, and voting in the opposite direction (i.e., down when previously voted up) undoes the vote as well.

Now for the problem. Voting once works like a charm and all the values are correctly saved to the database. Undoing a vote, however, refuses to work. To undo a vote I basically mark the vote to undo for deletion and then call SaveChanges on the context. As soon as I do that an InvalidOperationException occurs giving me the following message:

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.

I just don't get it. Down anyone know what I could be doing wrong? I've been Googleing for day but to no avail

A: 

Well, I've finally decided to work around it but simply mapping an int property instead of a ComplexType. I have also added a (non-mapped) helper property to avoid having to cast constantly from int to VoteType.

I would still love to get an answer for my problem so if you can help it I would appreciate it. I'll give it a couple of days before I mark my own answer as correct.

EDIT: Since I've gotten no answer whatsoever to this, m marking my own answer as good.

Sergi Papaseit