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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:00:07+00:00 2026-05-16T03:00:07+00:00

In the CategoriesTranslated collection i have this error: illegal access to loading collection. public

  • 0

In the CategoriesTranslated collection i have this error: illegal access to loading collection.

 public class Category : Entity
{
    public Category()
    {
        CategoriesTranslated = new List<CategoryTranslated>();

    }

    public virtual Category Parent { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<CategoryTranslated> CategoriesTranslated { get; set; }
}

public class CategoryTranslated : Entity
{
    public CategoryTranslated()
    {
    }

    public virtual Category Category { get; set; }
    public virtual LanguageType Language { get; set; }
    public virtual string Name { get; set; }
}


   public void Override(AutoMapping<Category> mapping)
    {
        mapping.HasMany(x => x.CategoriesTranslated)
            .Inverse()
            .Cascade.All();

    }

  public void Override(AutoMapping<CategoryTranslated> mapping)
    {
        mapping.References(x => x.Category);
    }

The SQL:

CREATE TABLE Category(
   [Id] smallint primary key identity(1,1),
   [Parent] smallint null,
   [Name] varchar(50) not null unique,
   )
    alter table [Category] add CONSTRAINT fk_Category_Category  
FOREIGN KEY(Parent) references Category (Id)
go


    CREATE TABLE CategoryTranslated(
   [Id] smallint primary key identity(1,1),
   [Category] smallint not null,
   [Language] tinyint not null,
   [Name] varchar(50) not null,
   )

    alter table [CategoryTranslated] add CONSTRAINT fk_CategoryTranslated_Category  
 FOREIGN KEY(Category) references Category (Id)
 go

Where is it wrong?

UPDATE
The links to the hbm generater:

Category:
http://uploading.com/files/fmb71565/SubmitSiteDirectory.Core.Category.hbm.xml/

Category translated:
http://uploading.com/files/9c9aaem9/SubmitSiteDirectory.Core.CategoryTranslated.hbm.xml/

  • 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-16T03:00:07+00:00Added an answer on May 16, 2026 at 3:00 am

    I am guessing it has to do with the creation of the list inside the constructor, especially if you left a default ctor for NHib. And that NHib is trying to set the list before it’s created. The other complication here is that you have a bi-directional relationship, and CategoryTranslated may be trying to get at the list before its created too.

    I doubt this is the only solution, but here is a pattern I use that should solve the error:

        /// <summary>Gets the ....</summary>
        /// <remarks>This is what is available to outside clients of the domain.</remarks>
        public virtual IEnumerable<CategoryTranslated> CategoriesTranslated{ get { return _InternalCategoriesTranslated; } }
    
        /// <summary>Workhorse property that maintains the set of translated categories by:
        /// <item>being available to <see cref="Category"/> to maintain data integrity.</item>
        /// <item>lazily instantiating the <see cref="List{T}"/> when it is needed.</item>
        /// <item>being the property mapped to NHibernate, the private <see cref="_categoriesTranslated"/> field is set here.</item>
        /// </list>
        /// </summary>
        protected internal virtual IList<Category> _InternalCategoriesTranslated
        {
            get { return _categoriesTranslated?? (_categoriesTranslated= new List<Category>()); }
            set { _categoriesTranslated= value; }
        }
        private IList<StaffMember> _categoriesTranslated;
    

    Now you need to set your mapping to access the private field, so assuming you use my casing preferences here, you’d have:

    public void Override(AutoMapping<Category> mapping)
    {
        mapping.HasMany(x => x.CategoriesTranslated)
            .Inverse()
            .Access.CamelCaseField(CamelCasePrefix.Underscore)
            .Cascade.All();
    }
    

    HTH,
    Berryl

    EDIT ============

    The _Internal collection also gives the child of of the bi-directional relationship, CategoryTranslated in this case, a hook, as shown in the code below:

        public virtual CategoryTranslated CategoryTranslated
        {
            get { return _categoryTranslated; }
            set
            {
                if (_categoryTranslated!= null)
                {
                    // disassociate existing relationship
                    _categoryTranslated._InternalCategoryTranslated.Remove(this);
                }
    
                _categoryTranslated= value;
    
                if (value != null)
                {
                    //make the association
                    _categoryTranslated._InternalCategoryTranslated.Add(this);
                }
            }
        }
        private CategoryTranslated _categoryTranslated;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.