title: MVC: The INSERT statement conflicted with the FOREIGN KEY constraint
body: <p>I have two tables that are supposed to be related.</p>
  
  <p><img src="https://i.stack.imgur.com/q5LBk.png" alt="enter image description here"></p>
  
  <p>Tables and Coloums Specification </p>
  
  <p>Primary key table<br>
  ProductCategory<br>
  ProductCategoryID       </p>
  
  <p>Foreign key table<br>
  SubProductCategory2
  ProductCategoryID</p>
  
  <p>In the controller I have the following methods when creating sub category...</p>
  
  <pre><code>public ActionResult Create()
  {
    ViewBag.ProductCategory = db.ProductCategories.OrderBy(p =&gt;  
    p.ProductCategoryID).ToList();
    ViewBag.SubProductCategory2 = db.SubProductCategory2.OrderBy(a =&gt; 
    a.ProductCategoryID).ToList();
  
    var PC2 = new SubProductCategory2();
    return View(PC2);
  } 
  
  public ActionResult Create(SubProductCategory2 Createsubcat2, 
  FormCollection values)
  {
    if (ModelState.IsValid)
    {
      db.AddToSubProductCategory2(Createsubcat2);
      db.SaveChanges(); 
        //error pointing here and the full error message I am getting is...
       /*error: System.Data.SqlClient.SqlException: 
       * The INSERT statement conflicted with the FOREIGN KEY constraint 
       * "FK_SubProductCategory2_ProductCategory". The conflict occurred in  
       * database "MyHouseDB", table "dbo.ProductCategory", column 
       * 'ProductCategoryID'. The statement has been terminated.*/    
       return RedirectToAction("/");
     }
  
     ViewBag.ProductCategory = db.ProductCategories.OrderBy(p =&gt; 
     p.ProductCategoryID).ToList();
     ViewBag.SubProductCategory2 = db.SubProductCategory2.OrderBy(a =&gt; 
     a.ProductCategoryID).ToList();
  
     return View(Createsubcat2);
    }
   ViewBag.ProductCategory = db.ProductCategories.OrderBy(p =&gt; 
      p.ProductCategoryID).ToList();
      ViewBag.SubProductCategory2 = db.SubProductCategory2.OrderBy(a =&gt; 
      a.ProductCategoryID).ToList();
      return View(Createsubcat2); 
  </code></pre>
  
  <p>in the views I have the following code...</p>
  
  <pre><code>&lt;div class="editor-label"&gt;
    @Html.LabelForModel()
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
     @Html.DropDownList("CategoryName", new   
     SelectList((System.Collections.IEnumerable)ViewData["ProductCategory"], 
     "ProductCategoryID", "CategoryName"))
     @Html.ValidationMessageFor(model =&gt; model.ProductCategory.CategoryName)
  </code></pre>
  
  <p></p>
  
  <p>Could some tell me how to solve the The INSERT statement conflicted with the FOREIGN KEY constraint error message. Correct me if I'm wrong, have I created the relationship between two tables incorrectly or the problem else where? Thanks in advance. </p>