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

  • Home
  • SEARCH
  • 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 8433653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:26:48+00:00 2026-06-10T06:26:48+00:00

Possible Duplicate: Delete object and all its child objects in Entity Framework? This code:

  • 0

Possible Duplicate:
Delete object and all its child objects in Entity Framework?

This code:

        int WebsiteID = int.Parse(Request.QueryString["id"]);
        Website websiteObj = db.Websites
            .SingleOrDefault(x => x.website_id == WebsiteID);

        foreach (BusinessObjects.Page pageObj in websiteObj.Pages)
        {
            foreach (SubPage subpageObj in pageObj.SubPages)
            {
                pageObj.SubPages.Remove(subpageObj);
            }
            websiteObj.Pages.Remove(pageObj);
        }

        foreach (Sector sectorObj in websiteObj.Sectors)
        {
            foreach (Product productObj in sectorObj.Products)
            {
                sectorObj.Products.Remove(productObj);
            }
            websiteObj.Sectors.Remove(sectorObj);
        }
        db.Websites.DeleteObject(websiteObj);

A website has multiple pages, a page has multiple subpages. A website also has multiple sectors and each sector has multiple products.

I want to delete the website and clear all relationships + entities related to it. I’m sure there are better ways to write the above.

Is there way to improve the logic?

  • 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-06-10T06:26:49+00:00Added an answer on June 10, 2026 at 6:26 am

    The simplest way, is to set up the relationships in the database as ON DELETE CASCADE

    Then the Linq becomes:

    var sites = db.Websites;
    var site_id = int.Parse(Request.QueryString["id"]);
    var site = sites.FirstOrDefault(x => x.website_id == site_id);
    sites.DeleteObject(site);
    

    The only real changes I’ve made to your linq apart from removing what is no longer needed if deletes cascade is:

    Using FirstOrDefault for a slight performance boost. This isn’t too big a deal with databases, as it just does a TOP 2 to implement. It’s worth not doing Single when First works, because in other cases it can mean having to examine every single object (only way to do Single against a List<T>)*. Of course, if there’s any possibility of there being two sites with the same id, then I’ve just introduced a bug, but then the real bug is in not having the id as a primary key to ensure that this could never happen.

    Taking Obj out of the name of the object. This seemed to be short for “object” and hence to be tautologous – every other variable in every piece of .NET code ever written is for an object, so there’s no point in pointing this out.

    *A bit more on this because I don’t want to encourage bad habits. When there can be only one matching object, then First gets that object while Single gets that object and also makes sure it is the only one (the same applies to the OrDefault variants except they differ when there is no object found at all).

    The only way to know there’s only one matching object, is to try to find at least two of them and then check you only got one. This is of minor impact with a database table if it’s indexed on columns relevant to your search (it does a TOP 2 instead of a TOP 1), but can be serious otherwise.

    The question is how important that is for that check to be made. Sometimes you don’t care; use First. Sometimes you should have a strong degree of confidence that there is only one such object; use First but do check on the reason for that confidence to make sure you don’t have a bug (in this case, check there’s a primary or unique key on the index column). If you really need to check then do use Single as using First in such a case is the very epitome of a bad optimisation – being faster but wrong is never the goal.

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

Sidebar

Related Questions

Possible Duplicate: Is it OK to use “delete this” to delete the current object?
Possible Duplicate: C++: Delete this? Object-Oriented Suicide or delete this; I wonder if the
Possible Duplicate: Undefined, unspecified and implementation-defined behavior I know calling delete on same object
Possible Duplicate: C++ delete - It deletes my objects but I can still access
Possible Duplicate: Delete a folder on SD card In my app i saved all
Possible Duplicate: How do I recursively delete a directory and its entire contents (files+sub
Possible Duplicate: What's the best way to delete all data from a table? Clear
Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) This should be
Possible Duplicate: Deleting Objects in JavaScript I have a JS object having a large
Possible Duplicate: Prolog delete: doesn't delete all elements that unify with Element In Prolog

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.