title: Entity Framework "A relationship multiplicity constraint violation occurred"
body: <p>I received this error from Entity Framework this morning:</p>
  
  <p><em>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.</em></p>
  
  <p>The error refers to a very simple One-To-Many relationship between <code>User</code> and <code>Address</code></p>
  
  <pre><code>public class User
  {
      public Guid Id { get; set; }
      public virtual IList&lt;Address&gt; Addresses { get; set; }
  } 
  
  public class Address
  {
      public Guid Id { get; set; }
      public User User { get; set; }
  } 
  </code></pre>
  
  <p>I'm using Fluent API to configure the relationship between entities, but in this case the relationship seems to simple that at first I didn't specify any particular rule and I let EF "deduce" the relationship:</p>
  
  <pre><code>public class AddressConfiguration : EntityTypeConfiguration&lt;Address&gt;
  {
      public AddressConfiguration()
      {
          HasKey(a =&gt; a.Id);
          Property(a =&gt; a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
      }
  }
  
  public class UserConfiguration : EntityTypeConfiguration&lt;User&gt;
  {
      public UserConfiguration()
      {
          HasKey(u =&gt; u.Id);
          Property(u =&gt; u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
      }
  }
  </code></pre>
  
  <p>But after receiving the error I tried to specify the following rule in the AddressConfiguration:</p>
  
  <pre><code>public class AddressConfiguration : EntityTypeConfiguration&lt;Address&gt;
  {
      public AddressConfiguration()
      {
          HasKey(a =&gt; a.Id);
          Property(a =&gt; a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
          HasOptional(x =&gt; x.User).WithMany(); // I tried adding this
      }
  }
  </code></pre>
  
  <p>After doing that, I tried generating a new automatic migration, and this is what I obtained:</p>
  
  <pre><code>public partial class AddressFixMigration : DbMigration
  {
      public override void Up()
      {
          DropForeignKey("dbo.Addresses", "User_Id", "dbo.Users");
          AddColumn("dbo.Addresses", "User_Id1", c =&gt; c.Guid());
          CreateIndex("dbo.Addresses", "User_Id1");
          AddForeignKey("dbo.Addresses", "User_Id1", "dbo.Users", "Id");
      }
  
      public override void Down()
      {
          DropForeignKey("dbo.Addresses", "User_Id1", "dbo.Users");
          DropIndex("dbo.Addresses", new[] { "User_Id1" });
          DropColumn("dbo.Addresses", "User_Id1");
          AddForeignKey("dbo.Addresses", "User_Id", "dbo.Users", "Id");
      }
  }
  </code></pre>
  
  <p>I found this very odd. The Db seemed ok even before, with the Addresses Table having a Foreign Key "User_Id", but after specifying the one-to-many relationship in the Configuration file EF wants to create a <em>different</em> Foreign Key. Why??</p>
  
  <p>I also tried to  specify <code>HasOptional(x =&gt; x.User).WithMany().Map(x =&gt; x.MapKey("User_Id"));</code>, but in that case when I try to create the automatic migration I receive the following error:</p>
  
  <p><em>User_Id: Name: Each property name in a type must be unique. Property name 'User_Id' is already defined.</em></p>
  
  <p>It seems like something is clearly wrong with my DB, but I can't see what and why.</p>