title: How to save changes to related table in MVC 5 and EF 6 - Entity Framework Master-Detail
body: <p>I have two tables, Promotion and PromotionLine, with a foreign key defined as PromotionLine.PromoID = Promotion.ID</p>
  
  <p>PromotionLines are associated with the Promotion model with the following in the Promotion class:</p>
  
  <pre><code>public IList&lt;PromotionLine&gt; PromotionLineItems { get; set; }
  </code></pre>
  
  <p>I chose not to use virtual because I don't want the promo lines loaded if we're just using a summary view to show high level Promotion info (such as a list of Promotions).</p>
  
  <p>When promotion details are needed, I get the promotion lines:</p>
  
  <pre><code>    public static Promotion GetInstance(int? promotionId)
      {
          if (promotionId == null) return null;
          using (APP01Entities entities = new APP01Entities())
          {
              return entities.Promotions
                  .Include(s =&gt; s.PromotionState)
                  .Include(h =&gt; h.PromotionHeaderType)
                  .Include(l =&gt; l.PromotionLineItems)
                  .Include(c =&gt; c.PromotionComments)
                  .FirstOrDefault(p =&gt; p.ID == promotionId);
          }
  
      }
  </code></pre>
  
  <p>This works, and I can access the promotion lines in my view.</p>
  
  <p>However, when I go to update changes, I encounter the error:</p>
  
  <blockquote>
    <p>“A referential integrity constraint violation occurred: The property
    value(s) of 'Promotion.ID' on one end of a relationship do not match
    the property value(s) of 'PromotionLine.PromotionID' on the other
    end.”</p>
  </blockquote>
  
  <p>I understand WHY this error is occurring.  I don't know how to get around it.
  I'm using the default update method (created by EF scaffolding):</p>
  
  <pre><code>public bool Update()
      {
          try
          {
              using (APP01Entities entities = new APP01Entities())
              {
                  entities.Promotions.Attach(this);
  
                  var entity = entities.ChangeTracker.Entries&lt;Promotion&gt;().FirstOrDefault(e =&gt; e.Entity == this);
  
                  if (entity == null)
                  {
                      return false;
                  }
                  entity.State = EntityState.Modified;
                  entities.SaveChanges();
              }
              return true;
          }
          catch (System.Data.Entity.Validation.DbEntityValidationException e)
          {
              throw e.Improve();
          }
      }
  </code></pre>
  
  <p><strong>The problem is with:</strong></p>
  
  <pre><code>entities.Promotions.Attach(this);
  </code></pre>
  
  <p>"this" has the promotion lines.  entities.Promotions does not.</p>
  
  <p>Here is how I'm calling the update method:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Edit(Promotion promotion)
      {
          if (ModelState.IsValid)
          {
              promotion.Update();
          }
          return View(promotion);
      }
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <ul>
  <li>How do I get the promotion lines added to entities.Promotions?</li>
  <li>or, should I be approaching this update differently?</li>
  </ul>