title: Violation of PRIMARY KEY constraint. Cannot insert duplicate key
body: <p>I'm using a <code>List</code> object to contain items from a secondary table that my primary table has a one-to-many relationship with. The <code>PK</code> of the primary table serves as the <code>FK</code> field on the secondary table. </p>
  
  <p>However, I'm getting this error when the record attempts to save</p>
  
  <pre><code>Violation of PRIMARY KEY constraint 'PK_ClearinghousePartners'. Cannot insert duplicate 
  key in object 'dbo.ClearinghousePartners'. The duplicate key value is (0)
  </code></pre>
  
  <p><strong>Primary Table Model</strong></p>
  
  <p>FYI - this table contains a LARGE number of fields so I'm just showing the <code>List</code></p>
  
  <pre><code>public partial class AgentTransmission
  { 
      .
      .
      public virtual List&lt;ClearinghousePartners&gt; ClearinghousePartners { get; set; }
  }
  </code></pre>
  
  <p><strong>Secondary Table Model</strong></p>
  
  <pre><code>public partial class ClearinghousePartners
  {
      public int Id { get; set; }
      public string ClearingHouseName { get; set; }
      public string TradingPartnerName { get; set; }
      public Nullable&lt;System.DateTime&gt; StartDate { get; set; }
      public int AgtTransId { get; set; } //FK field corresponds to 'Id' on AgentTransmission
  
      public virtual AgentTransmission AgentTransmission { get; set; }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <p>When the model posts back to the controller to save the exception is thrown in this area of the code. This is an instance in which the <code>AgentTransmission</code> object is completely new and needs to be added to the database. Same goes for each item in the <code>ClearinhousePartners</code> collection. </p>
  
  <p>Each are brand new <code>ClearinghousePartners</code> items with no values for <code>Id</code> and <code>AgtTransId</code> fields. I need the <code>AgentTransmission</code> object to save first so it's <code>Id</code> field can be created and THEN inserted into the <code>AgtTransId</code> field in the <code>ClearinghousePartners</code> objects.</p>
  
  <pre><code>agenttransmission.LastChangeDate = DateTime.Now;
  agenttransmission.LastChangeOperator = Security.GetUserName(User);
  db.AgentTransmission.Add(agenttransmission);
  db.SaveChanges(); //Exception thrown here
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;fieldset id="ClearinghousePartners"&gt;
      &lt;legend&gt;Clearinghouse Partners&lt;/legend&gt;
      &lt;center&gt;
          &lt;table&gt;
              &lt;thead&gt;
                  &lt;th&gt;Clearinghouse Name&lt;/th&gt;
                  &lt;th&gt;Trading Partner Name&lt;/th&gt;
                  &lt;th&gt;Start Date&lt;/th&gt;                             
              &lt;/thead&gt;
              &lt;tbody&gt;
                  @for (int i = 0; i &lt; Model.ClearinghousePartners.Count(); i++)
                  {
                      &lt;tr align="center"&gt;
                          @Html.HiddenFor(model =&gt; model.ClearinghousePartners[i].Id)
                          @Html.HiddenFor(model =&gt; model.ClearinghousePartners[i].AgtTransId)
                          &lt;td&gt;@Html.TextBoxFor(model =&gt; model.ClearinghousePartners[i].ClearingHouseName, new { style = "width: 100px" })&lt;/td&gt;
                          &lt;td&gt;@Html.TextBoxFor(model =&gt; model.ClearinghousePartners[i].TradingPartnerName, new { style = "width: 100px" })&lt;/td&gt;
                          &lt;td&gt;@Html.TextBoxFor(model =&gt; model.ClearinghousePartners[i].StartDate, new { style = "width: 100px" })&lt;/td&gt;                                    
                      &lt;/tr&gt;
                  }
              &lt;/tbody&gt;
          &lt;/table&gt;                            
      &lt;/center&gt;
  &lt;/fieldset&gt;
  </code></pre>