Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6086707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:47:11+00:00 2026-05-23T11:47:11+00:00

I was looking for a general solution for cascading delete in linq to sql

  • 0

I was looking for a general solution for cascading delete in linq to sql from c#, but I could not find any yet.

So I came up with my own solution, which handles one to many relations.

It would not be the general way to delete entities, but some edge case requires it. So before it goes to the production environment, I would like to know that what are you think about it?

It seems to work, but what are the pros and cons, where it can fail? Note that it only supposed to traverse the relation tree downside.

public class CascadingDeleteHelper
{
        public void Delete(object entity, Func<object, bool> beforeDeleteCallback)
        {
            if (!(entity is BusinessEntity))
            {
                throw new ArgumentException("Argument is not a valid BusinessEntity");
            }

            if (beforeDeleteCallback == null || beforeDeleteCallback(entity))
            {
                Type currentType = entity.GetType();
                foreach (var property in currentType.GetProperties())
                {
                    var attribute = property
                        .GetCustomAttributes(true)
                        .Where(a => a is AssociationAttribute)
                        .FirstOrDefault();

                    if (attribute != null)
                    {
                        AssociationAttribute assoc = attribute as AssociationAttribute;

                        if (!assoc.IsForeignKey)
                        {
                            var propertyValue = property.GetValue(entity, null);
                            if (propertyValue != null && propertyValue is IEnumerable)
                            {
                                IEnumerable relations = propertyValue as IEnumerable;
                                List<object> relatedEntities = new List<object>();

                                foreach (var relation in relations)
                                {
                                    relatedEntities.Add(relation);
                                }

                                relatedEntities.ForEach(e => Delete(e, beforeDeleteCallback));
                            }
                        }
                    }
                }

                SingletonDataContext.DataContext.GetTable(currentType).DeleteOnSubmit(entity);
                SingletonDataContext.DataContext.SubmitChanges();
            }
        }
    }

Thank you very much,

negra

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:47:11+00:00Added an answer on May 23, 2026 at 11:47 am

    Pros of your solution:

    • It’s generic
    • It does not require database changes on schema updates

    Cons:

    • Error prone (more custom code)
    • Performance is (most likely) not optimal

    There is a less complex, but more maintenance heavy solution, which is adding ON CASCADE DELETE rules into your database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a very specific question, but I'm looking for a slightly more general solution
I'm looking for a general solution for upgrading database schema with ORM tools, like
I've found a similar question here , but I'm looking for more general solutions.
I'm not looking for a general discussion on if math is important or not
I'm looking for a general strategy/advice on how to handle invalid UTF-8 input from
Looking for a general purpose WPF toolset. Currently looking at Telerik and Infragistics. Are
Looking at the general trend of comments in my question about Building an Aircraft
I'm looking for some general Optimization Correctness Extensibility advice on my current C++ Hierarchical
I'm looking for a general purpose API/web service/tool/etc... that allows convert a given HTML
Looking for a toolkit/SDK for general Blackberry development or application toolkits. Anything like an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.