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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:36:51+00:00 2026-05-31T19:36:51+00:00

this time i have a simple question: I have one table, related with himself

  • 0

this time i have a simple question:

I have one table, related with himself

=====================
| Id                |  
| IdParent          |
| NodeName          | 
=====================

Where IdParent Could Have 0 or many Id.
The question is, how can i configure this relationship using FluentApi or data anotations.

The result of this relationship is a tree (with two levels) like this:

a
|---b
|---c 
d
e
f---g    

where (a,d,e,f) are parent nodes and (b,c,g) are child nodes.

Very Tks for your 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-05-31T19:36:52+00:00Added an answer on May 31, 2026 at 7:36 pm

    Entity framework supports self-referencing foreign keys. It’s not clear if you are a database first, or codefirst person.

    If you’re a database first person, just create a self-referencing foreign key in your database, the update the model from the DB. EF has supported this style of key since at least EF 4.

    From a code first perspective, it may a bit trickier. If you’re using EF 4.x you need to override the OnModelCreating function (see example here). If you’re using EF 5 (from the .net 4.5 beta, or the June 2011 CTP), then this question provides some good guidance. The short of it is to use [ForeignKey("IdParent")] attribute on your model.

    Either way you should see XML something like …

      <edmx:Runtime>
        <edmx:StorageModels>
        <Schema Namespace="...">
              <AssociationSet Name="FK_Culture_has_ParentCulture" 
                       Association="AL_Model.Store.FK_Culture_has_ParentCulture">
                <End Role="Culture" EntitySet="Culture" />
                <End Role="Culture1" EntitySet="Culture" />
              </AssociationSet>
    
     .....
     .....
     <edmx:ConceptualModels>
        <Schema Namespace="...">
        <EntityContainer Name="..." >
          <Association Name="FK_Culture_has_ParentCulture">
            <End Role="Culture" Type="AL_Model.Store.Culture" Multiplicity="0..1" />
            <End Role="Culture1" Type="AL_Model.Store.Culture" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Culture">
                  <PropertyRef Name="CultureID" />
                </Principal>
                <Dependent Role="Culture1">
                  <PropertyRef Name="ParentCultureID" />
                </Dependent>
              </ReferentialConstraint>
          </Association>
    

    Self-Referencing Structure

    This statement warrents a little attention:

    Where IdParent Could Have 0 or many Id. 
    

    In order to have “zero, or a parentID” then you’re going to need to have an entry in your table containing zero as an ID. Why? because foreign keys need to link to something. What I suspect you want, however, is to make IdParent nullable. Your question doesn’t specify database, but most major databases enforce foreign keys only when there is a value. Said another way, Null in IdParent means there is no parent.

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

Sidebar

Related Questions

Surprisingly can't find a match for my question. I have one table that I
A quick question. I have a simple table with this structure: USERS_TABLE = {id},
I'm re-asking this question but with a different framework this time. I have two
I have tough time making this design decision. I could go with traditional new
Simple and fast question, i have those tables: //table people | pe_id | pe_name
I have a very simple question, but can't seem to find a strait answer
I have an HBase schema-design related question. The problem is fairly simple - I
This time I have an error which I have been trying again to figure
this time it's about Ruby On Rails. I have a form in the view
I have been building enterprise software for the last 10 years. In this time

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.