LinqDataSource Cascading Delete from a GridView

Answered LinqDataSource Cascading Delete from a GridView

  • Friday, October 19, 2007 5:43 PM
     
     

     

    Does anyone know of a good way to do a Cascading Delete from a GridView using a LinqDataSource?

     

    I currently have a relationship that looks like Customer has an Address.  When I delete customer I would like to delete the orphaned address at the same time.  The delete from the LinqDataSource doesn't seem to take this relationship into account.  Any ideas? 

     

    Thanks,

     

    John

All Replies

  • Tuesday, October 23, 2007 4:37 AM
     
     

    As far as I know, you can do the cascading delete in database side, for example sql server has such a setting in foreign key settings.

    Thanks

  • Tuesday, October 23, 2007 6:03 AM
     
     Answered

    You can uset the DeleteCustomer() Partial method of the DataContext class to perform the cascaded Delete

    For example

     

    Code Block

    public partial class MyDataContext

    {

    partial void DeleteCustomer(Customer instance)

    {

    var DeletedAddresses = from a in Address

    where a.CustomerID = instance.CustomerID

    select a;

    Adress.RemoveAll(DeletedAddresses);

    SubmittChanges();

    //or use this line if you have an association

    Address.RemoveAll(instance.Address);

    Submittchanges();

    }