title: How to fix: The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical?
body: <p>I am using Entity Framework 4.3.1 against a SQL Server 2012 database and I am using the POCO approach.  I am getting the following error and I am wondering if anyone can explain how to fix it:</p>
  
  <p><strong>ModelValidationException</strong></p>
  
  <blockquote>
    <blockquote>
      <p>One or more validation errors were detected during model generation:
      \tSystem.Data.Entity.Edm.EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.</p>
    </blockquote>
  </blockquote>
  
  <p>There is no <code>InnerException</code> available for any further information.</p>
  
  <p>I cannot change the database schema and it is a little odd, but here it is...</p>
  
  <ul>
  <li>** are the primary key (notice I have composite primary keys)</li>
  <li>(FK) Denotes a foreign key</li>
  </ul>
  
  <p>Here are the tables (if it helps I can post the SQL to generate them but I do not think the tables are actually the problem as the exception is in the validation of the model):</p>
  
  <pre><code>One
  -
  **OneId int not null
  **TwoId int not null (FK)
  **ThreeId int not null (FK)
  Name nvarchar(50) not null
  
  Two
  -
  **TwoId int not null
  **ThreeId int not null (FK)
  Name nvarchar(50) not null
  
  Three
  -
  **ThreeId not null
  Name nvarchar(50) not null
  </code></pre>
  
  <p>Here are the entities (notice that I am including the foreign keys in the model but other than that pretty standard):</p>
  
  <pre><code>public class Three
  {
      public int ThreeId { get; set; }
      public string Name { get; set; }
      public virtual ICollection&lt;Two&gt; Twos { get; private set; }
      public virtual ICollection&lt;One&gt; Ones { get; private set; }
  
      public void AddOne(One one)
      {
          if (one == null)
              throw new ArgumentNullException("two");
  
          if (Ones == null)
              Ones = new List&lt;One&gt;();
  
          if (!Ones.Contains(one))
              Ones.Add(one);
  
          one.Three = this;
      }
  
      public void AddTwo(Two two)
      {
          if (two == null)
              throw new ArgumentNullException("two");
  
          if (Twos == null)
              Twos = new List&lt;Two&gt;();
  
          if (!Twos.Contains(two))
              Twos.Add(two);
  
          two.Three = this;
      }
  }
  
  public class Two
  {
      public int TwoId { get; set; }
      public int ThreeId { get; set; }
      public string Name { get; set; }
      public virtual Three Three { get; set; }
      public virtual ICollection&lt;One&gt; Ones { get; private set; }
  
      public void AddOne(One one)
      {
          if (one == null)
              throw new ArgumentNullException("two");
  
          if (Ones == null)
              Ones = new List&lt;One&gt;();
  
          if (!Ones.Contains(one))
              Ones.Add(one);
  
          one.Two = this;
      }
  }
  
  public class One
  {
      public int OneId { get; set; }
      public int TwoId { get; set; }
      public int ThreeId { get; set; }
      public virtual Two Two { get; set; }
      public virtual Three Three { get; set; }
  }
  </code></pre>
  
  <p>And here is the data context:</p>
  
  <pre><code>public class DbCtx : DbContext
  {
      public DbCtx(string connectionString)
          : base(connectionString)
      {
          Ones = Set&lt;One&gt;();
          Twos = Set&lt;Two&gt;();
          Threes = Set&lt;Three&gt;();
      }
  
      public DbSet&lt;One&gt; Ones { get; private set; }
      public DbSet&lt;Two&gt; Twos { get; private set; }
      public DbSet&lt;Three&gt; Threes { get; private set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          var one = modelBuilder.Entity&lt;One&gt;();
          one.ToTable("One");
  
          one.HasKey(d =&gt; new
                              {
                                  d.OneId,
                                  d.TwoId,
                                  d.ThreeId
                              });
  
          one.Property(d =&gt; d.OneId)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
  
          one.HasRequired(t =&gt; t.Two)
              .WithMany(s =&gt; s.Ones)
              .HasForeignKey(t =&gt; t.TwoId);
  
          one.HasRequired(t =&gt; t.Three)
              .WithMany(s =&gt; s.Ones)
              .HasForeignKey(t =&gt; t.ThreeId);
  
          var two = modelBuilder.Entity&lt;Two&gt;();
          two.ToTable("Two");
  
          two.HasKey(d =&gt; new
                              {
                                  d.TwoId,
                                  d.ThreeId
                              });
  
          two.Property(p =&gt; p.TwoId)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
  
          two.HasRequired(t =&gt; t.Three)
              .WithMany(s =&gt; s.Twos)
              .HasForeignKey(t =&gt; t.ThreeId);
  
          var three = modelBuilder.Entity&lt;Three&gt;();
          three.ToTable("Three");
          three.HasKey(s =&gt; s.ThreeId);
  
          three.Property(p =&gt; p.ThreeId)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
  
          base.OnModelCreating(modelBuilder);
      }
  }
  </code></pre>
  
  <p>Finally, this is a snippet of code to cause the exception:</p>
  
  <pre><code>using (var ctx = new DbCtx(@"....."))
  {
      Console.WriteLine(ctx.Twos.Count());
  }
  </code></pre>