title: DbContext: Dependent property / ReferentialConstraint Exception when using Composite Key
body: <p>I'm using EntityFramework via DbContext and an Exisiting Database.</p>
  
  <p>When I Add an Order entity to my context and call SaveChanges(), I'm encountering an exception of: "A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: OrderId".  </p>
  
  <p>I believe this is happening because of the composite key on my OrderAddress table and I'm hoping there is a way around it...I don't want to create an IDENTITY on that table.</p>
  
  <p>Here are my entities, simplified...</p>
  
  <pre><code>// OrderId is an IDENTITY PK
  public class Order
  {
       public int OrderId { get; set; }
       public IList&lt;OrderAddress&gt; Addresses { get; set; }
       public int Total { get; set; }
  }
  
  // PK on this table is composite key of OrderId and OrderAddressTypeId
  public class OrderAddress
  {
       public int OrderId { get; set; }
       public int OrderAddressTypeId { get; set; }
       public string Address { get; set; }
  }
  </code></pre>
  
  <p>Here is my Context, simplified...</p>
  
  <pre><code>public class StoreContext : DbContext
  {
       DbSet&lt;Order&gt; Orders { get; set; }
       DbSet&lt;OrderAddress&gt; OrderAddresses { get; set; }
  
       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
            // Set the Identity for Order
            modelBuilder.Entity&lt;Order&gt;()
                 .Property(x =&gt; x.OrderId)
                 .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
            // Set composite key for Order Address
            modelBuilder.Entity&lt;OrderAddress&gt;()
                 .HasKey(x =&gt; new { x.OrderId, x.OrderAddressTypeId });
       }
  }
  </code></pre>
  
  <p>NOTE: I've looked at the other SO questions that are similar and tried the solutions listed with no success.  I've verified that my foreign keys are setup correctly in the database.  What's different about my question is the use of the composite key.</p>
  
  <p>Thanks in advance for the help.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>This ended up not being related to the composite key at all.  There was an additional line in my Context OnModelCreating method that required a child entity, OrderSummary, which is based on a View in my database.  The line looked like this...</p>
  
  <pre><code>modelBuilder.Entity&lt;OrderSummary&gt;().HasRequired(x =&gt; x.Order).WithRequiredPrincipal(x =&gt; x.OrderSummary);
  </code></pre>
  
  <p>I had never intended for OrderSummary to be a required principal of Order.  Changing it to the following fixed the problem...</p>
  
  <pre><code>modelBuilder.Entity&lt;OrderSummary&gt;().HasRequired(x =&gt; x.Order);
  </code></pre>
  
  <p>Unfortunately, the error message from EF was not very specific and lead me on a wild good chase.</p>
  
  <p>Thanks for looking.</p>