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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:12:48+00:00 2026-06-12T23:12:48+00:00

I have an entity, let’s call it CommonEntity that has a primary key used

  • 0

I have an entity, let’s call it CommonEntity that has a primary key used as a foreign key in many other entities. As the application is developed these links will continue to grow.

I’d like a way to see if CommonEntity can be safely deleted (i.e. it’s not used by any other entities).

I realise I can do

if(!ce.EntityA.Any() && !ce.EntityB.Any() ... && !ce.EntityN.Any())
{
   //Delete
}

but I’m hoping for a way to just check all of the relationships automatically, as I don’t love the idea of having to come back and change this code manually every time we add a new relationship. Perhaps there is something in EF4+ that I’m not aware of?

I thought it might be possible to use a transaction scope to just try and delete the object and roll it back if it fails, but I wasn’t sure if there were any adverse side effects with this approach.

Is there a better approach?

EDIT: Looks like VS2012 has used EF5 even though the project is .Net 4, so it has created the model with POCOs even though it was generated from a DB.

  • 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-12T23:12:49+00:00Added an answer on June 12, 2026 at 11:12 pm

    Just let it fail. If the entity has many relationships, that verification could be really heavy.

    public bool TryDelete(int id)
    {
        try
        {
            // Delete
            return true;
        }
        catch (SqlException ex)
        {
            if (ex.Number == 547) return false; // The {...} statement conflicted with the {...} constraint {...}
            throw; // other error
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have an entity that has attributes att1 and att2, where att1
Let say I have an entity user which has a one to many relationship
Let's say that hypothetically I have a table called Products with a primary key
I have a table that has two different many-to-many relations to two different tables.
Let's say I have an entity called EntityA . EntityA has a relationship to
Let's say I have an entity Author which has a name can write 0
Let's say I have an entity that looks like this: public class Album() {
Let's say I have an entity called Game which has a home_school_id . I
I have a problem. I used to have an entity that had a @id
Let's say I have an Entity in my Entity Framework Model that is named

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.