jason
jason

Reputation: 767

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

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.

Upvotes: 3

Views: 9447

Answers (2)

Leo
Leo

Reputation: 419

The problem is that you are initializing PlayerBaseballStat in the Player constructor. Similar question here

This needs to go away:

// initialize with empty shell

    PlayerBaseballStat = new PlayerBaseballStat();

Upvotes: 0

Derek Van Cuyk
Derek Van Cuyk

Reputation: 953

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.

Upvotes: 1

Related Questions