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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:14:26+00:00 2026-05-17T01:14:26+00:00

I’m trying to create a self referencing object using linqTOsql mapping. So far, I

  • 0

I’m trying to create a self referencing object using linqTOsql mapping. So far, I am definitely in over my head. Here’s the code I have:

[Table]
public class Category
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    public Int64 catID { get; set; }
    public Int64 parentCatID { get; set; }
    public string catName { get; set; }
    public string catDescription { get; set; }

    internal EntityRef<IEnumerable<Category>> _category;
    [Association(ThisKey = "parentCatID", Storage = "_category")]
    public IEnumerable<Category> category {
        get { return _category.Entity; }
        set { _category.Entity = value; }
    }
}

My fakeRepository is defined like this:

// Fake hardcoded list of categories
private static IQueryable<Category> fakeCategories = new List<Category> {
    new Category { catID = 1, parentCatID = 0, catName = "Root", catDescription = "" },
    new Category { catID = 2, parentCatID = 1, catName = "Category w/subs", catDescription = "" },
    new Category { catID = 3, parentCatID = 1, catName = "Category no subs but now has subs", catDescription = "" },
    new Category { catID = 4, parentCatID = 2, catName = "Zub Cat", catDescription = "" },
    new Category { catID = 5, parentCatID = 2, catName = "Sub Cat", catDescription = "" },
    new Category { catID = 6, parentCatID = 0, catName = "Another Root", catDescription = "" },
    new Category { catID = 7, parentCatID = 0, catName = "Ze German Root", catDescription = "" },
    new Category { catID = 8, parentCatID = 3, catName = "Brand new cats", catDescription = "" },
    new Category { catID = 9, parentCatID = 8, catName = "Brand new cats sub", catDescription = "" },
}.AsQueryable();

I pass Category to the view like this:

public ActionResult CategoryTree()
{
    IQueryable<Category> cats = genesisRepository.Category
                                                 .Where(x => x.parentCatID == 0)
                                                 .OrderBy(x => x.catName);
    return View(cats);
}

The problem that I’m running into is that all of this compiles, but I don’t get anything beyond the root categories. Model[0].category is returning null.

What is wrong with my self-referencing object?

Edit

I wonder if it’s not working because I don’t have a real linq-to-sql data context in my fakeRepository. If that’s the case, is there a way around that? Can I can get this to work without a connection to a database?

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

    Yeah, you hit the nail on the head. It’s not working because you’re using a fake repository.

    Linq-to-Sql does all the wiring up for you and sets the related collections based on the properties (& their attributes) that you setup in your model.

    I don’t know how to accomplish this without a connection to the database because internal EntityRef<IEnumerable<Category>> _category; is completely foreign to me – I’m more of a POCO model type of guy.

    After a quick google, I found this – How to: Map Database Relationships (LINQ to SQL)

    Could you change your model to read:

    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    public Int64 CatId { get; set; }
    [Column]
    public Int64 ParentCatId { get; set; }
    [Column]
    public string CatName { get; set; }
    [Column]
    public string CatDescription { get; set; }
    
    private EntitySet<Category> _ChildCategories;
    [Association(Storage = "_ChildCategories", OtherKey = "ParentCatId")]
    public EntitySet<Category> ChildCategories
    {
        get { return this._ChildCategories; }
        set { this._ChildCategories.Assign(value); }
    }
    
    private EntityRef<Category> _ParentCategory;
    [Association(Storage = "_ParentCategory", ThisKey = "ParentCatId")]
    public Category ParentCategory
    {
        get { return this._ParentCategory.Entity; }
        set { this._ParentCategory.Entity = value; }
    }
    

    Now because your ChildCategories is of type EntitySet<Category> (which inherits from IList<T>) you should be able to wire fake relationships up yourself.

    So you could do something like this:

    private static IQueryable<Category> GetFakeCategories()
    {
        var categories = new List<Category> {
            new Category { CatId = 1, ParentCatId = 0, CatName = "Root", CatDescription = "" },
            new Category { CatId = 2, ParentCatId = 1, CatName = "Category w/subs", CatDescription = "" },
            //Blah
            new Category { CatId = 8, ParentCatId = 3, CatName = "Brand new cats", CatDescription = "" },
            new Category { CatId = 9, ParentCatId = 8, CatName = "Brand new cats sub", CatDescription = "" }
        };
    
        //Loop over the categories to fake the relationships
        foreach (var category in categories)
        {
            category.ChildCategories = new EntitySet<Category>(); //new up the collection
            foreach (var subLoopCategory in categories)
            {
                if (category.ParentCatId == subLoopCategory.CatId)
                    category.ParentCategory = subLoopCategory;
    
                if (category.Id == subLoopCategory.ParentCatId)
                    category.ChildCategories.Add(subLoopCategory);
            }
        }
        return categoies.AsQueryable();
    }
    

    It works in my head at least… 🙂

    HTHs,
    Charles

    EDIT: Re: Comment below about a null reference on _childCategories.

    You could change the model to look like:

    private EntitySet<Category> _ChildCategories = new EntitySet<Category>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.