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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:02:50+00:00 2026-05-30T09:02:50+00:00

NHibernate really does not seem to like returning a ReadOnlyCollection, depsite me having implemented

  • 0

NHibernate really does not seem to like returning a ReadOnlyCollection, depsite me having implemented what I’ve read in about 30 places as the correct access strategy for a read only collection backed to a private field.

I have the following code in my entity:

private readonly IList<TagAlias> _aliases = new List<TagAlias>();

public IEnumerable<TagAlias> Aliases
{
    get
    {
        return new ReadOnlyCollection<TagAlias>(this._aliases); 
    }
}

and the following mapping to allow access to the backing field

public sealed class TagMap : ClassMap<Tag>
{
    public TagMap()
    {
        Table("Tag");
        Id(x => x.Id).Column("TagId").GeneratedBy.Identity();
        Map(x => x.Value).Column("TagName");
        HasMany(x => x.Aliases)
            .AsSet()
            .Access.ReadOnlyPropertyThroughCamelCaseField(Prefix.Underscore)
            .KeyColumn("TagId")
            .LazyLoad()
            .Inverse()
            .Cascade.AllDeleteOrphan();
    }
}

Why on earth does NHibernate still insist on getting the backing list via the ready only collection when I do something like .Clear(), rather than accessing it like I have told it to in the mapping? I am loath to change my domain model just for the sake of the persistence layer, but NHibernate just doesn’t seem to be wanting to cooperate.

The error I get is “A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance” but it goes away when I just return this._aliases in the property getter.

  • 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-30T09:02:51+00:00Added an answer on May 30, 2026 at 9:02 am

    I generally use collections like this and don’t have problems:

    private IList<OrderLine> orderLines;
    public virtual IEnumerable<OrderLine> OrderLines 
    { 
        get { return orderLines.Select(x => x); } 
    }
    
    HasMany(x => x.OrderLines)
                .Access.CamelCaseField()
                .KeyColumn("ORDER_ID")
                .Inverse()
                .Cascade.AllDeleteOrphan();
    

    I’m not sure of exactly how you are using your collections to get this error but I generally have add and remove methods for my collections inside the classes. If this doesn’t help you maybe you could post the example that causes that error.

    Here is a related article I posted several months back:

    Exposing HasMany and ManyToMany relationships as IEnumerable

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

Sidebar

Related Questions

Hi Im curious about how DDD is really implemented using Fluent Nhibernate. for example
NHibernate is not really a good fit for our environment due to all the
So I really like working with NHibernate but always used Spring.Net with it. I
What does really POCO mean, in respect to dependencies ? With NHibernate; child collections
Background I really like Fluent NHibernate - it's pretty great. I don't have to
We're extensively using NHibernate multi-query functionality and experiencing strange behavior. Seems like NHibernate does
Using NHibernate from C# and only HQL (not SQL) in a way that is
Using NHibernate.Mapping.Attributes, I have a entity class with something like: [Class] public class EntityA
Does nHibernate create code that has all the details of the column? I know
In this question I asked about NHibernate session lifetime. I'm using a desktop application,

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.