title: EF Code First: Multiplicity constraint violation
body: <p>HELP! - I am receiving the following error on my navigational model <strong>ArticleType</strong>:</p>
  
  <blockquote>
    <p>A relationship multiplicity constraint violation occurred</p>
  </blockquote>
  
  <p>Here is the existing database schema:</p>
  
  <p><img src="https://i.imgur.com/6BGGTx3.png" alt="database model"></p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class Article
  {
      public int ID { get; set; }
      public virtual Stage Stage { get; set; }
      public virtual ArticleType ArticleType { get; set; } // Causes the violation
  }
  
  public class ArticleType
  {
      public int ID { get; set; }
      public string Title { get; set; }
  }
  
  public class Stage
  {
      public int ID { get; set; }
      public string Title { get; set; }
  }
  </code></pre>
  
  <p>I use fluent api for my mapping, heres an excerpt of the association</p>
  
  <pre><code>// This works
  modelBuilder.Entity&lt;Article&gt;
      .HasRequired(t =&gt; t.Stage)
      .WithMany() // if turned .WithOptional() then will also cause the error.
      .Map(m =&gt; m.MapKey("stage_id"));
  
  // This does not work
  modelBuilder.Entity&lt;Article&gt;
      .HasRequired(t =&gt; t.ArticleType)
      .WithMany()
      .Map(m =&gt; m.MapKey("article_type_id"));
  </code></pre>
  
  <p>My problem is that why is it that the <strong>ArticleType</strong> is causing the error but the <strong>Stage</strong> does not, even when both's declaration and mapping are syntactically the same?</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I found out about  the exception by examining the Article object by hovering my mouse (not sure the exact term)</p>
  
  <p><img src="https://i.stack.imgur.com/Q8PF3.png" alt="open image to new tabe"></p>
  
  <p><strong>Error Detail</strong></p>
  
  <pre><code>System.InvalidOperationException was unhandled by user code
    HResult=-2146233079
    Message=A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.
    Source=System.Data.Entity
    StackTrace:
         at System.Data.Objects.DataClasses.EntityReference`1.Load(MergeOption mergeOption)
         at System.Data.Objects.DataClasses.RelatedEnd.Load()
         at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()
         at System.Data.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)
         at System.Data.Objects.Internal.LazyLoadBehavior.&lt;&gt;c__DisplayClass7`2.&lt;GetInterceptorDelegate&gt;b__2(TProxy proxy, TItem item)
         at System.Data.Entity.DynamicProxies.Package_A18FADC105CCF13C9CD346622D43BD35514E489CCC1E5B1E4A3C78806BDCA0F5.get_ArticleType()
         at AuthorProofing.Service.ReminderService.DeliverDailyReminders() in C:\Users\default.Lenovo-PC\Documents\Visual Studio 2010\Projects\AuthorProofing\AuthorProofing.Service\ReminderService.cs:line 36
         at AuthorProofing.Tests.ReminderServiceTest.DeliverDailyRemindersTest() in C:\Users\default.Lenovo-PC\Documents\Visual Studio 2010\Projects\AuthorProofing\AuthorProofing.Tests\ReminderServiceTest.cs:line 76
    InnerException: 
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I decided to go with explicit foreign key associations.</p>
  
  <pre><code>class ArticleMap : EntityTypeConfiguration&lt;Article&gt;
  {
      public ArticleMap()
      {
          // Primary Key
          this.HasKey(t =&gt; t.ID);
          this.Property(t =&gt; t.ID)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
          // Strongly typed FK properties
          this.Property(t =&gt; t.StageID).IsRequired();
          this.Property(t =&gt; t.ArticleTypeID).IsRequired();
  
          // Navigation Models
          this.HasRequired(t =&gt; t.Stage);
          this.HasRequired(t =&gt; t.ArticleType);
  
          // Table &amp; Column Mappings
          this.ToTable("items");
          this.Property(t =&gt; t.ID).HasColumnName("item_id");
          this.Property(t =&gt; t.StageID).HasColumnName("stage_id");
          this.Property(t =&gt; t.ArticleTypeID).HasColumnName("article_type_id");
      }
  }
  
  
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Configurations.Add(new ArticleTypeMap());
      modelBuilder.Configurations.Add(new StageMap());
      modelBuilder.Configurations.Add(new ArticleMap());
  }
  </code></pre>
  
  <p>Still does not work.</p>
  
  <p>Using the new foreign key mapping approach, I tried to switch the keys <strong>article_type_id</strong> and <strong>stage_id</strong>. </p>
  
  <pre><code>this.Property(t =&gt; t.StageID).HasColumnName("article_type_id"); // &lt;-- Switched
  this.Property(t =&gt; t.ArticleTypeID).HasColumnName("stage_id");  // &lt;-- Switched
  </code></pre>
  
  <p>Suddenly, the error disappeared. Currently baffled right now. I think somehow the ArticleType model does not like the foreign key "article_type_id".</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>After adding the <code>.HasForeignKey(...)</code> on my navigational model mappings, I received a new error: <strong>Unknown column 'ArticleType_ID' in 'field list'</strong></p>
  
  <pre><code>class ArticleMap : EntityTypeConfiguration&lt;Article&gt;
  {
      public ArticleMap()
      {
          // Primary Key
          this.HasKey(t =&gt; t.ID);
          this.Property(t =&gt; t.ID)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
          // Foreign Key Properties
          this.Property(t =&gt; t.StageID)
              .IsRequired();
          this.Property(t =&gt; t.JournalID)
              .IsRequired();
          this.Property(t =&gt; t.ArticleTypeID)
              .IsRequired();
  
          // Navigational Models
          this.HasRequired(t =&gt; t.Stage); // This works
          this.HasRequired(t =&gt; t.ArticleType)
              .WithMany()
              .HasForeignKey(t =&gt; t.ArticleTypeID); // Newly added
  
          // Table &amp; Column Mappings
          this.ToTable("items");
          this.Property(t =&gt; t.ID).HasColumnName("item_id");
          this.Property(t =&gt; t.ArticleTypeID).HasColumnName("article_type_id");
          this.Property(t =&gt; t.StageID).HasColumnName("stage_id");
      }
  }
  
  class ArticleTypeMap : EntityTypeConfiguration&lt;ArticleType&gt;
  {
      public ArticleTypeMap()
      {
          // Primary Key
          this.HasKey(t =&gt; t.ID);
  
          // Properties
          this.Property(t =&gt; t.ID)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
          this.Property(t =&gt; t.Title)
              .HasMaxLength(100)
              .IsRequired();
  
          // Table &amp; Column Mappings
          this.ToTable("article_types");
          this.Property(t =&gt; t.ID).HasColumnName("article_type_id"); // &lt;-- Apparently, this is no longer mapped.
          this.Property(t =&gt; t.Title).HasColumnName("title");
      }
  }
  </code></pre>