Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiplicity constraint violated. The role xxx of the relationship xxx has multiplicity 1 or 0..1
    primarykey
    data
    text
    <p>Ive seen this question asked a few times but I'm afraid I am just not understanding the answers.</p> <p>In a nutshell, I have a Player that contains a property of type PlayerBaseballStat. Its a 1 to 1 relationship. PlayerBaseballStat has an <code>ICollection&lt;BaseballStat&gt;</code> property. both PlayerBaseballStat and BaseballStat have PlayerId to join the relationships. </p> <pre><code> 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&lt;BaseballStat&gt; 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; } } </code></pre> <p>The mappings are:</p> <pre><code> modelBuilder.Entity&lt;Player&gt;() .HasOptional(p =&gt; p.PlayerBaseballStat); modelBuilder.Entity&lt;PlayerBaseballStat&gt;() .HasRequired&lt;Player&gt;(x =&gt; x.Player); modelBuilder.Entity&lt;PlayerBaseballStat&gt;() .HasMany(p =&gt; p.BaseballStats); modelBuilder.Entity&lt;BaseballStat&gt;() .HasKey(x =&gt; new { x.PlayerId, x.Year, x.StatName }); modelBuilder.Entity&lt;BaseballStat&gt;() .HasRequired(x =&gt; x.Player); </code></pre> <p>The data access layer is a templated repository. The multiplicity constraint violation occurs on a read operation.</p> <pre><code>public T Get(Expression&lt;Func&lt;T, bool&gt;&gt; where) { return dbset.Where(where).FirstOrDefault&lt;T&gt;(); } </code></pre> <p>The actual read is</p> <pre><code>_playerBaseballStatsRepository.Get(x =&gt; x.PlayerId == playerId); </code></pre> <p>That translates to</p> <pre><code>dataContext.PlayerBaseballStats.FirstOrDefault(x =&gt; x.PlayerId == playerId); </code></pre> <p>I know I am missing something in the mappings but I just cannot seem to figure it out. Please help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload