Multiplicity constraint violated. The role xxx of the relationship xxx has multiplicity 1 or 0..1

c# data-binding entity-framework

Question

Ive seen this question asked a few times but I'm afraid I am just not understanding the answers.

In a nutshell, I have a Player that contains a property of type PlayerBaseballStat. Its a 1 to 1 relationship. PlayerBaseballStat has an ICollection<BaseballStat> property. both PlayerBaseballStat and BaseballStat have PlayerId to join the relationships.

 public class Player
    {
        public Player()
        {
            // initialize with empty shell
            PlayerBaseballStat = new PlayerBaseballStat();
        }
        public int PlayerId { get; set; }

//other properties removed for brevity

        public virtual PlayerBaseballStat PlayerBaseballStat { get; set; }

    }

public class PlayerBaseballStat 
    {
        [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int PlayerId { get; set; }
        public virtual Player Player { get; set; }

//other properties removed for brevity
       public virtual ICollection<BaseballStat> BaseballStats { get; set; }

    }
public class BaseballStat : EntityBase
    {
        public int PlayerId { get; set; }
        public virtual Player Player { get; set; }
        public int Year { get; set; }

        [MaxLength(25)]
        [Required]
        public string StatName { get; set; }
        [Required]
        public decimal StatValue { get; set; }


    }

The mappings are:

  modelBuilder.Entity<Player>()
            .HasOptional(p => p.PlayerBaseballStat);

        modelBuilder.Entity<PlayerBaseballStat>()
          .HasRequired<Player>(x => x.Player);

        modelBuilder.Entity<PlayerBaseballStat>()
            .HasMany(p => p.BaseballStats);

        modelBuilder.Entity<BaseballStat>()
            .HasKey(x => new { x.PlayerId, x.Year, x.StatName });

        modelBuilder.Entity<BaseballStat>()
            .HasRequired(x => x.Player);

The data access layer is a templated repository. The multiplicity constraint violation occurs on a read operation.

public T Get(Expression<Func<T, bool>> where)
    {
        return dbset.Where(where).FirstOrDefault<T>();
    }

The actual read is

_playerBaseballStatsRepository.Get(x => x.PlayerId == playerId);

That translates to

dataContext.PlayerBaseballStats.FirstOrDefault(x => x.PlayerId == playerId);

I know I am missing something in the mappings but I just cannot seem to figure it out. Please help.

1
2
3/15/2013 12:10:21 PM

Accepted Answer

What code specifically is causing the issue? Is it when you update an existing entity, insert a new entity, or simply retrieving a specific entity? It would help if you could post some sample code too. I created a small demo app with the schema provided above (minus the EntityBase inheritance on the BaseballStat entity) and executed the following flawlessly:

var player = new Player()
    {
        PlayerBaseballStat = new PlayerBaseballStat()
        {
            BaseballStats = new Collection<BaseballStat>()
            {
                new BaseballStat()
                { 
                    StatName = "ERA", 
                    StatValue = 1.41M, 
                    Year = 2011
                }                                            
            }
        }
    };

    context.Players.Add(player);
    context.SaveChanges();

It could possibly be how you are initializing the object.

1
3/15/2013 4:08:21 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow