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 6166827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:24:23+00:00 2026-05-23T22:24:23+00:00

I have a class that gets arranged into a hierarchy, such as this: class

  • 0

I have a class that gets arranged into a hierarchy, such as this:

class TestContainer
{
... bla bla bla...
  public Virtual Item Items {get;set;}
}
Class Item
{
[Key]
public int ID {get;set;}
public string PropA {get;set;}
public string PropB {get;set;}



[InverseProperty("Parent")]
public virtual ICollection<Item> Children { get; set; }

[InverseProperty("Contents")]
public virtual Item Parent { get; set; }
}

So, I have an update method that lets a user upload an updated set of children.

I didnt realize at first but deleting my container TestContainer doesnt delete the items.

Ok, No problem right?

I inserted the following functions (which is probably very ugly but I am still in the prototyping phase here)

   private void DeleteItems(TestContainer existingContainer)
        {

            //setup a flat list for deletion
            List<Item> trashCan = new List<Item>();
            //delete the old CI's from the database
            BuildDeletionList(existingContainer.Items, ref trashCan);

            foreach (Item item in trashCan)
            {
                context.Items.Remove(item);
            }
        }


        private void BuildDeletionList(ICollection<Item> items, ref List<Item> trashCan)
        {
            if (items != null)
            {
                foreach (Item item in items)
                {

                    BuildDeletionList(item, ref trashCan);
                }
            }

        }

        private void BuildDeletionList(Item item, ref List<Item> trashCan)
        {

            if (Item.Children != null)
            {
                foreach (Item child in Item.Children)
                {
                    BuildDeletionList(item, ref trashCan);
                }
            }
        item.Children.clear();
            trashCan.Add(item);
        }

The problem here is that this either crashes my test server, or when I remove the recursive bit (and only remove parents — testing out what is going wrong at this point), I get the following error

“An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception.”

How can I best cascade delete an Item and all of its Children, so that I do not have orphans in the database? (As I do now)

  • 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-23T22:24:24+00:00Added an answer on May 23, 2026 at 10:24 pm

    Instead of deleting an entity and its decedents using EF you can enable Cascade delete on the foreign key on your database. It is much more efficient than generating multiple delete statements.

    Edit

    You need to add WillCascadeOnDelete() when you configure the model

            modelBuilder.Entity<Item>().HasMany(i => i.Children)
                .WithOptional(i => i.Parent)
                .HasForeignKey(i => i.ParentId)
                .WillCascadeOnDelete();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that gets arranged into a hierarchy, such as this: class
I have a class that looks like this public class SomeClass { public SomeChildClass[]
This is a two part question. I have a class that gets all processes
I have a C# class that gets loaded with a database row (orm)..this object
I have this method in an exterior class that gets called whenever the charachter
So I have a class that gets items from a database and then places
I have a C++ template class that gets instantiated with 3 different type parameters.
Let's say I have a class that looks something like this: class Foo(Prop1:Int, Prop2:Int,
I have a wizard class that gets used a lot in my program. Unfortunately,
I have a class Foo that a property of the type Bar. public class

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.