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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:11:13+00:00 2026-06-06T00:11:13+00:00

I have a self-referential table in my database that looks sort of like above.

  • 0

Sample Table

I have a self-referential table in my database that looks sort of like above. Basically its setup in such a way that each row has a unique ID (identity PK) and a DependentID to indicate any other record in the set that it is dependent on. It is very similar to the parent-child type examples you often see in SQL textbooks but my case is subtly unique in the sense that a given record can also be dependent upon itself (see row 1 above)

Two questions:

  1. Can EF be made to represent this relationship properly? I’ve read several posts on here that suggest that it does not deal with this scenario gracefully so my initial thought was that it might not even be worth it, I might be better off just treating it as a normal table and writing the business logic to ensure the data gets inserted/updated correctly. In my scenario, I won’t ever be querying these entities thru EF really, the app will basically load them all at startup and then I’ll run linq queries against them at runtime to filter as needed

  2. Assuming I cannot get it to work with EF and as I note in #1 I simply load em all up into memory at startup (there are only going to be 50-100 or so), what would be the most efficient way to join on this via linq? I would want to be able to pass in a DependentId and get all the records associated with it and their properties…so in this example I’d want to pass in ‘1’ and get back:

1 – John – 10

2 – Mike – 25

3 – Bob – 5

thanks for the help

  • 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-06T00:11:15+00:00Added an answer on June 6, 2026 at 12:11 am

    Indeed, the entity framework cannot represent such a relationship, certainly not in in a recursively queryable form.

    But you are not asking for recursive queries, so you could treat DependentId as just another data column. Doing that, it would be trivial to build and execute your question-two query against the database.

    UPDATE:

    That query would look something like

    int dependentIdToSearch = 1;
    
    var q = from something in db.mytable
            where something.DependentId == dependentIdToSearch
            select new { something.Id, something.Name, something.Value };
    

    END UPDATE

    If you do need recursive queries (all direct and indirect dependencies of), you need a table valued function with a common table expression. The entity framework cannot deal with that either, at least not in the current version. If you need this support, you can wait for EF 5 or use Linq to SQL (which had support for table valued functions since the first version years ago).

    You can indeed also read the entire table in memory, provided that it is read-only, or that there is only “one memory” (single server, not load-balanced or client app with local database).

    If it’s read-only, you have the option to build an object graph once at load time, enabling efficient execution later. For example, you could define a class with a collection of objects that are dependent on each object. Your query then becomes a trivial iteration over that collection.

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

Sidebar

Related Questions

Here's what I have: I have a table called Area that is self referential.
I have a self referential table that I am mapping with kodo jdo 4
I have a data table that contains a one-to-many self referential relationship: Plant {
I have a self referencing table named categories that has a parentcategoryid column that
I have a self referencing table in Oracle 9i, and a view that gets
I have a self referential table, which has ID, ParentID (nullable). So, the table
I'm having issues with downcasting in JAXB with classes that are self-referential. My setup:
I have a self referential association like this: class Influencer < ActiveRecord::Base has_many :followers,
I have a self-referential MySQL table with a recursive parent_id: CREATE TABLE `recursive` (
If I have a self-referential Topic class and I want to recursively loop through

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.