Introduction
This article will discuss alternative methods for cascading deletes using LINQ to SQL. Cascading delete refers to removing records associated with a foreign key relationship to a record that is the target of a deletion action. LINQ to SQL does not explicitly handle cascading deletes; it is up to the developer to determine whether or not that action is desired. It is also up to the developer to determine how to accomplish the cascading delete.
Problem
The problem with performing a cascading delete is not new to LINQ to SQL, and one has essentially the same alternatives for performing such a delete. The issue is determining how to handle the deletion or retention of records associated with a record targeted for deletion where that record maintains a foreign key relationship with records contained within other tables within the database and, more specifically, where the foreign key fields are not nullable.
As an example, consider the customer table within the Northwind database. The customer table has a foreign key relationship established with the Orders table (which maintains a foreign key relationship with the Order_Details table). To delete a customer with associated Orders, one needs to dispose of or otherwise handle the associated records in both the Orders and Order_Details tables. The related tables are called entity sets in the LINQ to SQL jargon.
LINQ to SQL will not violate the foreign key relationships. If an application attempts to delete a record with such relationships in place, the executing code will throw an exception.
Using the Northwind example, an exception would occur if one attempts to delete a customer with associated orders. That is not a problem; that is how it should be; otherwise, why have foreign key relationships at all? The issue is determining if you want to delete records with associated entity sets. If you do, how would you like to handle it - do you want to keep the associated records or delete them right along with the targeted record?

Figure 1. Customers, Orders, and Order Details - Northwind Database
Solution
There are several possible alternatives at your disposal. You can handle the cascading deletes using LINQ to SQL from within your code or the foreign key relationships from within SQL Server.
If you were to execute this code against the Northwind database, it would create a customer with an associated order and order details.
try
{
Customer c = new Customer();
c.CustomerID = "AAAAA";
c.Address = "554 Westwind Avenue";
c.City = "Wichita";
c.CompanyName = "Holy Toledo";
c.ContactName = "Frederick Flintstone";
c.ContactTitle = "Boss";
c.Country = "USA";
c.Fax = "316-335-5933";
c.Phone = "316-225-4934";
c.PostalCode = "67214";
c.Region = "EA";
Order_Detail od = new Order_Detail();
od.Discount = .25f;
od.ProductID = 1;
od.Quantity = 25;
od.UnitPrice = 25.00M;
Order o = new Order();
o.Order_Details.Add(od);
o.Freight = 25.50M;
o.EmployeeID = 1;
o.CustomerID = "AAAAA";
c.Orders.Add(o);
using (NWindDataContext dc = new NWindDataContext())
{
var table = dc.GetTable<Customer>();
table.InsertOnSubmit(c);
dc.SubmitChanges();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
But if you then tried to delete the customer without handling the entity sets using something like this.
using (NWindDataContext dc = new NWindDataContext())
{
var q =
(from c in dc.GetTable<Customer>()
where c.CustomerID == "AAAAA"
select c).Single<Customer>();
dc.GetTable<Customer>().DeleteOnSubmit(q);
dc.SubmitChanges();
}
It would result in an error, and no changes would be made to the database.



Gowtham RajamanickamPosted Apr 11, 2015, 1:28 PM
Good one