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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:14:31+00:00 2026-05-13T11:14:31+00:00

I have run into an interesting problem with Entity Framework and based on the

  • 0

I have run into an interesting problem with Entity Framework and based on the code I had to use to tackle it I suspect my solution is less than ideal. I have a 1-to-Many relationship between Table A and Table B where entities in TableB have a reference to TableA. I have a scenario where I want to simultaneously delete all children of a row in TableA and I thought this could be achieve by simply clearing the collection:

Entity.Children.Clear()

Unfortunately, when I attempted to save changes this produced as a Foreign Key violation.

A relationship is being added or
deleted from an AssociationSet
‘FK_EntityB_EntityA’. With cardinality
constraints, a corresponding ‘EntityB’
must also be added or deleted.

The solution I came up with was to manually delete object via the entity context’s DeleteObject(), but I just know this logic I am using has got to be wrong.

while (collection.Any())
    Entities.DeleteObject(collection.First());

For one, the fact that I had to use a Where() loop seems far less than ideal, but I suppose that’s purely a semantic assessment on my part. In any case, is there something wrong with how I am doing this, or is there perhaps a better way to clear a child entity collection of an entity such that Entity Framework properly calls a data store delete on all of the removed objects?

  • 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-13T11:14:31+00:00Added an answer on May 13, 2026 at 11:14 am

    Clear() removes the reference to the entity, not the entity itself.

    If you intend this to be always the same operation, you could handle AssociationChanged:

    Entity.Children.AssociationChanged += 
        new CollectionChangeEventHandler(EntityChildrenChanged);
    Entity.Children.Clear();            
    
        private void EntityChildrenChanged(object sender,
            CollectionChangeEventArgs e)
        {
            // Check for a related reference being removed. 
            if (e.Action == CollectionChangeAction.Remove)
            {
                Context.DeleteObject(e.Element);
            }
        }
    

    You can build this in to your entity using a partial class.

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

Sidebar

Ask A Question

Stats

  • Questions 288k
  • Answers 288k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SQL Server has had exception support since 2005: BEGIN TRY… May 13, 2026 at 5:24 pm
  • Editorial Team
    Editorial Team added an answer No, that syntax isn't available. The best you could do… May 13, 2026 at 5:24 pm
  • Editorial Team
    Editorial Team added an answer I wrote a program (unfortunately now lost) to do something… May 13, 2026 at 5:24 pm

Related Questions

As many young programmers do, I learned the usefulness of inserting numerous print-to-console statements
I've come into an interesting problem (as is often the case in interacting with
We've run into an interesting situation that needs solving, and my searches have turned
So I have an interesting problem that's been the fruit of lots of good
This is an Erlang question. I have run into some unexpected behavior by io:fread.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.