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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:37:22+00:00 2026-06-07T23:37:22+00:00

So I have a SQL table which is basically ID, ParentID, MenuName, [Lineage, Depth]

  • 0

So I have a SQL table which is basically

ID, ParentID, MenuName, [Lineage, Depth]

The last two columns are auto-computed to help with searching so we can ignore them for now.

I’m creating a drop down menu system with multiple categories.

Unfortunately EF I don’t think plays nice with Self referencing tables more than 1 level deep. So I’m left with a few options

1) Create query, order by depth and then create a custom class in C#, populating it one depth at a time.

2) Find some way to eager load the data in EF, I don’t think it is possible for an unlimited amount of levels, only a fixed amount.

3) Some other way I’m not even sure about.

Any inputs would be welcomed!

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

    I have successfully mapped hierarchical data using EF.

    Take for example an Establishment entity. This can represent a company, university, or some other unit within a larger organizational structure:

    public class Establishment : Entity
    {
        public string Name { get; set; }
        public virtual Establishment Parent { get; set; }
        public virtual ICollection<Establishment> Children { get; set; }
        ...
    }
    

    Here is how the Parent / Children properties are mapped. This way, when you set the Parent of 1 entity, the Parent entity’s Children collection is automatically updated:

    // ParentEstablishment 0..1 <---> * ChildEstablishment
    HasOptional(d => d.Parent)
        .WithMany(p => p.Children)
        .Map(d => d.MapKey("ParentId"))
        .WillCascadeOnDelete(false); // do not delete children when parent is deleted
    

    Note that so far I haven’t included your Lineage or Depth properties. You are right, EF doesn’t work well for generating nested hierarchical queries with the above relationships. What I finally settled on was the addition of a new gerund entity, along with 2 new entity properties:

    public class EstablishmentNode : Entity
    {
        public int AncestorId { get; set; }
        public virtual Establishment Ancestor { get; set; }
    
        public int OffspringId { get; set; }
        public virtual Establishment Offspring { get; set; }
    
        public int Separation { get; set; }
    }
    
    public class Establishment : Entity
    {
        ...
        public virtual ICollection<EstablishmentNode> Ancestors { get; set; }
        public virtual ICollection<EstablishmentNode> Offspring { get; set; }
    
    }
    

    While writing this up, hazzik posted an answer that is very similar to this approach. I’ll continue writing up though, to provide a slightly different alternative. I like to make my Ancestor and Offspring gerund types actual entity types because it helps me get the Separation between the Ancestor and Offspring (what you referred to as Depth). Here is how I mapped these:

    private class EstablishmentNodeOrm : EntityTypeConfiguration<EstablishmentNode>
    {
        internal EstablishmentNodeOrm()
        {
            ToTable(typeof(EstablishmentNode).Name);
            HasKey(p => new { p.AncestorId, p.OffspringId });
        }
    }
    

    … and finally, the identifying relationships in the Establishment entity:

    // has many ancestors
    HasMany(p => p.Ancestors)
        .WithRequired(d => d.Offspring)
        .HasForeignKey(d => d.OffspringId)
        .WillCascadeOnDelete(false);
    
    // has many offspring
    HasMany(p => p.Offspring)
        .WithRequired(d => d.Ancestor)
        .HasForeignKey(d => d.AncestorId)
        .WillCascadeOnDelete(false);
    

    Also, I did not use a sproc to update the node mappings. Instead we have a set of internal commands that will derive / compute the Ancestors and Offspring properties based on the Parent & Children properties. However ultimately, you end up being able to do some very similar querying as in hazzik’s answer:

    // load the entity along with all of its offspring
    var establishment = dbContext.Establishments
        .Include(x => x.Offspring.Select(y => e.Offspring))
        .SingleOrDefault(x => x.Id == id);
    

    The reason for the bridge entity between the main entity and its Ancestors / Offspring is again because this entity lets you get the Separation. Also, by declaring it as an identifying relationship, you can remove nodes from the collection without having to explicitly call DbContext.Delete() on them.

    // load all entities that are more than 3 levels deep
    var establishments = dbContext.Establishments
        .Where(x => x.Ancestors.Any(y => y.Separation > 3));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we have a SQL query which will basically help in viewing table and
I have a SQL table in which some columns, when viewed in SQL Server
I have a SQL Server table which has 625 columns created with different datatypes
I have an sql table which has the following rows. (4081, 3, '', 'contrapreneurship.jpg',
I have a simple SQL table which defines a set of hierarchical categories and
I have a table which contains my ads that can be searched in sql-server-2008.
I have an SQL Server 2005 table that has a varchar(250) field which contains
I am using Microsoft SQL Server. I have a Table which had been updated
SQL Server 2005 I have an SQL Function (ftn_GetExampleTable) which returns a table with
I have a table in SQL Server 2005 which has a date time field.

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.