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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:27:56+00:00 2026-05-26T15:27:56+00:00

please help me with the following problem. i have a class, like public class

  • 0

please help me with the following problem. i have a class, like

public class TopicItem
{
    public TopicItem()
    {
        _children = new List<TopicItem>();
    }
    public int Id { get; set; }
    public string Title { get; set; }
    public int? ParentId { get; set; }
    public TopicItem Parent { get; set; }
    public List<TopicItem> Children { get; set; }
}

and fluent

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<TopicItem>().HasKey(t => t.Id);
    modelBuilder.Entity<TopicItem>().Property(t => t.Title).IsRequired();
    modelBuilder.Entity<TopicItem>().Property(t => t.Alias);
    modelBuilder.Entity<TopicItem>().HasOptional(t => t.Parent).WithMany(t => t.Children).HasForeignKey(t => t.ParentId);
    //modelBuilder.Entity<TopicItem>().HasMany(t => t.Children).WithOptional(t => t.Parent).HasForeignKey(t => t.ParentId);
    base.OnModelCreating(modelBuilder);
}

but it isn’t working. I’m trying to use it as follows:

public void Recursion(List<TopicItem> items)
{
    if (items != null)
    {
        foreach (var item in items)
        {
            Process(item);
        }
        Recursion(item.Children);
    }
}

Recursion(MyContext.TopicItems.Where(t => t.Parent == null).ToList())

but the children property for each parent entity is null.

is it possible to do it?

  • 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-26T15:27:57+00:00Added an answer on May 26, 2026 at 3:27 pm

    You must either make both Parent and Children virtual to support lazy loading (it can trigger really lot of queries to database) or you must explicitly tell EF to load Children by using eager loading. The problem with eager loading is that it doesn’t work recursively it will load only as many levels as you tell EF.

    EF and its query mechanism is not very suitable for recursive relations (tree structures).

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

Sidebar

Related Questions

Please help me with the following problem: I have the following classes: class ChemicalElement
Guys, please help me with the following problem. I have encountered the famous cannot
Please help me out with an algorithm for the following problem - Given a
Help me please with the following problem: - (NSDictionary *)getGamesList { NSMutableDictionary *gamesDictionary =
Please help regarding the following issue. I have enabled the Block popup option in
Please kindly help me in the following, I have $sub = C:\views\sght\gzad\text\hksdk\akldls\hool.java = C:\views\sght\bdsk\text\hksdfg\sdjks\same.java
Please can someone help? I have the following code which uploads a file to
I have been frustrated by this for like a day...please help. I have an
I have the following problem / bug: I made a custom button-class (called CustomBlitButton)
public static class clsCounter { static int count; public static int Counter { get

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.