title: NHibernate and ReadOnlyCollection<T>
body: <p>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.</p>
  
  <p>I have the following code in my entity:</p>
  
  <pre><code>private readonly IList&lt;TagAlias&gt; _aliases = new List&lt;TagAlias&gt;();
  
  public IEnumerable&lt;TagAlias&gt; Aliases
  {
      get
      {
          return new ReadOnlyCollection&lt;TagAlias&gt;(this._aliases); 
      }
  }
  </code></pre>
  
  <p>and the following mapping to allow access to the backing field </p>
  
  <pre><code>public sealed class TagMap : ClassMap&lt;Tag&gt;
  {
      public TagMap()
      {
          Table("Tag");
          Id(x =&gt; x.Id).Column("TagId").GeneratedBy.Identity();
          Map(x =&gt; x.Value).Column("TagName");
          HasMany(x =&gt; x.Aliases)
              .AsSet()
              .Access.ReadOnlyPropertyThroughCamelCaseField(Prefix.Underscore)
              .KeyColumn("TagId")
              .LazyLoad()
              .Inverse()
              .Cascade.AllDeleteOrphan();
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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 <code>this._aliases</code> in the property getter.</p>