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

  • Home
  • SEARCH
  • 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 6183951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:27:09+00:00 2026-05-24T01:27:09+00:00

I have been tasked with building a new system that models a virtual filesystem.

  • 0

I have been tasked with building a new system that models a virtual filesystem. My client has mandated that this be built using Entity Framework. I have handled node-like data structures before, but never with Entity Framework.

What is the best approach to build a hierarchical class in Entity Framework? By hierarchical, I mean that a class can have a parent of the same type, and can have zero or more children of the same type.

I’m using SQL Server 2008 and Entity Framework 4.0. Should I use the built-in hierarchy data type, or do I go the ParentID route? Suggestions would be most welcome.

  • 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-24T01:27:11+00:00Added an answer on May 24, 2026 at 1:27 am

    I had the same problem. I found that the best way to work with hierarchyid datatype and still be using EF 4.0 is building a view over the the hierarchy table.

    Since the view is not updateable I created a delete, insert and add stored procedures and mapped them to the entity mapping in the ORM. This is working really good.

    Lets say you have this table:

    CREATE TABLE [dbo].[NodeHierarchy]
    (
    [Node] hierarchyid NOT NULL,
    [NodeId] int NOT NULL,
    [Level] AS ([Node].[GetLevel]()) PERSISTED,
    [Lineage] AS ([Node].[ToString]()) PERSISTED,
    [RootNode] AS ([Node].[GetAncestor]([Node].[GetLevel]() - 1)) PERSISTED,
    [ParentNode] AS ([Node].[GetAncestor](1)) PERSISTED
    )
    

    Now you create this view over it:

    CREATE VIEW [dbo].[NodeHierarchyView]
    AS
    SELECT   ch.NodeId AS [NodeId],
           ch.Node.ToString() AS [Lineage],
     ch.[Level] AS [Level],
     chr.Node.ToString() AS [RootLineage],
     chr.NodeId AS [RootNodeId],
     chp.Node.ToString() As [ParentLineage],
     chp.NodeId AS [ParentNodeId]
    FROM     dbo.NodeHierarchy ch
     LEFT OUTER JOIN NodeHierarchy chr ON
          ch.RootNode = chr.Node
     LEFT OUTER JOIN CompanyHierarchy chp ON
          ch.ParentNode = chp.Node
    

    Now I can create an entity in the model over the view and use Linq-to-Entities and get good performace and neat code.

    This is the add stored procedure I use:

    CREATE PROCEDURE [dbo].[AddNode]
    @NodeId int,
    @ParentNodeId int
    AS
    DECLARE @NewNode hierarchyid;
    DECLARE @ParnetLineage nvarchar(4000);
    
    SELECT  @ParnetLineage = Lineage
    FROM    NodeHierarchy
    WHERE   NodeId = @ParentNodeId
    
    IF @ParnetLineage IS NULL
    BEGIN
        SET @ParnetLineage = N'/';
    END
    
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
    BEGIN TRANSACTION
        SET @NewNode = CAST(@ParnetLineage + CAST(@NodeId AS nvarchar (4000)) + N'/' AS hierarchyid);
    
        INSERT NodeHierarchy (Node, NodeId)
        VALUES (@NewNode, @NodeId)
    COMMIT
    
    SELECT @NodeId AS [NewNodeId]
    RETURN 0
    

    I created all the needed indexes and constraints over the tables. In my solution the view is displaying data from other tables and the procedures manipulate these tables as well.

    Oded

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

Sidebar

Related Questions

I have been tasked with building a new workflow system to handle our service
I have been tasked with building a new web project from scratch, with the
I have been tasked with going through a number of ColdFusion sites that have
I have been tasked with developing a solution that tracks changes to a database.
I have been tasked with creating a program that will generate an amortization schedule.
I have been tasked with developing a new retail e-commerce storefront for my current
I have been tasked with implementing a PKI library in C# for a company
I have been tasked with coming up with a compatibility guide for SharePoint 2007
I have been tasked to optimize some sql queries at work. Everything I have
I have been tasked with finding an open source DOM XML parser. The parser

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.