title: Delete records from multiple tables with foreign key constraint
body: <p>I'm using Asp.Net MVC and building an E-commerce application. </p>
  
  <p>I've 4 tables in my DB</p>
  
  <p><code>Product</code> which have <code>ProductId(PK), Name, Price, StoreId(FK)</code></p>
  
  <p><code>Store</code> which have <code>StoreId(PK), Name, CreatedOn</code></p>
  
  <p><code>Order</code> which have <code>OrderId(PK), StoreId(FK), OrderDate, OrderTotal</code></p>
  
  <p><code>OrderDetail</code> which have <code>OrderDetailId(PK), OrderId(FK), Quantity, UnitPrice, Weight</code></p>
  
  <p>When I Try to delete a single product, it doesn't give any error and deleted the product, If I delete <code>Order</code> record it gives an error of OrderDetails foreign key, I know  I have to delete OrderDetail record first then Order record.</p>
  
  <p>My scenario is, Store has multiple products, Each store has orders then order details.</p>
  
  <p>Now, I don't want to delete single record every time because it takes alot of time. I want to delete a STORE and all the record from other tables (Product, Order, OrderDetails) will also be deleted. How can I do this ?</p>
  
  <p><strong>I've tried this</strong> This code is in my IdentityModel.cs</p>
  
  <pre><code>protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Product&gt;()
              .HasRequired(s =&gt; s.Store)
              .WithMany()
              .HasForeignKey(d =&gt; d.StoreId)
              .WillCascadeOnDelete(false); // also tried with TRUE, didn't work
  
          modelBuilder.Entity&lt;Order&gt;()
              .HasRequired(s =&gt; s.Store)
              .WithMany()
              .HasForeignKey(d =&gt; d.StoreId)
              .WillCascadeOnDelete(false); // also tried with TRUE, didn't work
  
          modelBuilder.Entity&lt;OrderDetail&gt;()
              .HasRequired(s =&gt; s.Order)
              .WithMany()
              .HasForeignKey(d =&gt; d.OrderId)
              .WillCascadeOnDelete(false); // also tried with TRUE, didn't work
      }
  </code></pre>
  
  <p><strong>Controller</strong> </p>
  
  <pre><code>[HttpPost]
      public ActionResult StoreDelete(int? id, Store store)
      {
          try
          {
              if (id == null)
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  
              store = db.Stores.Find(id);
  
              if (store == null)
              {
                  return HttpNotFound();
              }
              else
              {
                  db.Stores.Remove(store);
                  db.SaveChanges();
                  return RedirectToAction("ManageStores", "Store");  
              }
  
          }
  
          catch
          {
              return View();
          }
      }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@using (Html.BeginForm("StoreDelete", "Store", new { id = ViewBag.StoreId }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
  {
  @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  &lt;h4 class="text-danger"&gt;Are you sure you want to delete this store ?&lt;/h4&gt;
  &lt;br /&gt;
  @Html.TextBoxFor(m =&gt; m.Name, new { @class = "inputboxes texbboxstyle form-control", @readonly = "readonly" })
  &lt;br&gt;
  @Html.TextBoxFor(m =&gt; m.CreatedOn, new { @class = "inputboxes texbboxstyle form-control", @readonly = "readonly" })
  &lt;br&gt;
  &lt;input type="submit" class="btn-lg btn-danger" style="border-radius:0px; width:200px;" value="Delete" /&gt;
  }
  </code></pre>