title: The DELETE statement conflicted with the REFERENCE constraint "FK__tbl8_update__HID__55F4C372"
body: <p>I have the following 2 tables</p>
  
  <pre><code>CREATE TABLE HOLIDAY_DATE_TABLE
  (
  HID INT IDENTITY PRIMARY KEY,
  TABLE_NUMBER nchar(2) NOT NULL,
  HOLIDAY_DATE nchar(8) NOT NULL,
  FIELD_DESCRIPTION nVARchar(43) NULL,
  );
  
  
  CREATE TABLE tbl8_update_transactions
  (
  TID INT IDENTITY PRIMARY KEY,
  TABLE_NUMBER nchar(2) NOT NULL,
  HOLIDAY_DATE nchar(8) NOT NULL,
  FIELD_DESCRIPTION nVARchar(43) NULL,
  HID int,
  FOREIGN KEY (HID) REFERENCES HOLIDAY_DATE_TABLE (HID)
  );
  </code></pre>
  
  <p>When I perform a delete via my application view (Delete.cshtml), I get the following error:</p>
  
  <p>"The DELETE statement conflicted with the REFERENCE constraint "FK__tbl8_update__HID__55F4C372". The conflict occurred in database "BillingUI", table "tbl8_update_transactions", column 'HID'.
  The statement has been terminated."</p>
  
  <p>Basically, when a delete is performed on a particular row from my view, I want it to delete the same record in both HOLIDAY_DATE_TABLE and tbl8_update_transactions. I read that I have to delete the child and then the parent. I tried changing the order in which the deletes take place, however when I do this it deletes the wrong record from tbl8_update_transactions because it's looking at its primary key for identification and not its foreign key. My code is below, thanks.</p>
  
  <pre><code> public ActionResult Delete(int id = 0)
      {
          HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
          //tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(id);
          if (holiday_date_table == null)
          {
              return HttpNotFound();
          }
          return View(holiday_date_table);
      }
  
      //
      // POST: /Table8/Delete/5
  
      [HttpPost, ActionName("Delete")]
      public ActionResult DeleteConfirmed(int id)
      {
          HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
          db.HOLIDAY_DATE_TABLE.Remove(holiday_date_table);
          db.SaveChanges();
          tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(id);
          db.tbl8_update_transactions.Remove(tbl8_update_transaction);
          db.SaveChanges();
          return RedirectToAction("../Billing/HolidayDateTable");
      } 
  </code></pre>
  
  <p><strong>Update:</strong> I think I wasn't clear enough. I tried reversing them already, i.e;</p>
  
  <pre><code>public ActionResult Delete(int id = 0)
      {
          HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
          //tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(id);
          if (holiday_date_table == null)
          {
              return HttpNotFound();
          }
          return View(holiday_date_table);
      }
  
      //
      // POST: /Table8/Delete/5
  
      [HttpPost, ActionName("Delete")]
      public ActionResult DeleteConfirmed(int id)
      {          
          tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(id);
          db.tbl8_update_transactions.Remove(tbl8_update_transaction);
          db.SaveChanges();
          HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
          db.HOLIDAY_DATE_TABLE.Remove(holiday_date_table);
          db.SaveChanges();
          return RedirectToAction("../Billing/HolidayDateTable");
      }
  </code></pre>
  
  <p>but it doesn't work, I receive the error "Value cannot be null.
  Parameter name: entity", pointing to 
      db.tbl8_update_transactions.Remove(tbl8_update_transaction);</p>
  
  <p>This is because (at least I think) that it's deleting from tbl8_update_transaction where it's unique identifier TID is equal to the value of HID  in HOLIDAY_DATE_TABLE. I need it to delete from tbl8_update_transaction using its foreign key value and am not sure how in C#. </p>
  
  <p><strong>Update 2:</strong> I tried having it identify the row by the HID (foreign key) value in tbl8_update_transaction...</p>
  
  <pre><code> public ActionResult Delete(int id = 0, int HID = 0)
      {
          HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
          tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(HID);
          if (holiday_date_table == null)
          {
              return HttpNotFound();
          }
          return View(holiday_date_table);
      }
  
      //
      // POST: /Table8/Delete/5
  
      [HttpPost, ActionName("Delete")]
      public ActionResult DeleteConfirmed(int id, int HID)
      {          
          tbl8_update_transactions tbl8_update_transaction = db.tbl8_update_transactions.Find(HID);
          db.tbl8_update_transactions.Remove(tbl8_update_transaction);
          db.SaveChanges();
          HOLIDAY_DATE_TABLE holiday_date_table = db.HOLIDAY_DATE_TABLE.Find(id);
          db.HOLIDAY_DATE_TABLE.Remove(holiday_date_table);
          db.SaveChanges();
          return RedirectToAction("../Billing/HolidayDateTable");
      }
  </code></pre>
  
  <p>But get the following error:
  The parameters dictionary contains a null entry for parameter 'HID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult DeleteConfirmed(Int32, Int32)' in 'BillingApp.Controllers.Table8Controller'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
  Parameter name: parameters  </p>