title: NHibernate is turning my collection readonly. How can I stop it?
body: <p>I'm having a bit of an issue with Nhibernate / Fluent NHibernate</p>
  
  <p>I have a class that has a collection and a backing field, and methods for manipulating the collection like so:</p>
  
  <p><strong>Edit: I've added the virtual modifier to <code>Children</code> since I forgot to stick it in the example code below (it was 2 am)</strong></p>
  
  <pre><code>public class MyClass
  {
      private IList&lt;SomeChildObject&gt; children;
  
      public virtual IList&lt;SomeChildObject&gt; Children { get { return new ReadOnlyCollection&lt;SomeChildObject&gt;(children); } }
  
      public void AddToChildren(SomeChildObject obj)
      {
          children.Add(obj);
      }
  }
  </code></pre>
  
  <p>And I have my Fluent NHibernate mapping like this:</p>
  
  <pre><code>public class MyClassMapping : ClassMap&lt;MyClass&gt;
  {
      public MyClassMapping()
      {
          HasMany(x =&gt; x.Children)
              .Inverse()
              .LazyLoad()
              .Cascade.AllDeleteOrphan()
              .KeyColumnNames.Add("MyClassID")
              .Access.AsReadOnlyPropertyThroughCamelCaseField();
      }
  }
  </code></pre>
  
  <p>Now all is good when I pull back an instance of MyClass from the database.</p>
  
  <pre><code>MyClass myClass = repo.GetById(12);
  
  myClass.AddToChildren(new SomeChildObject());
  </code></pre>
  
  <p>This works fine.</p>
  
  <p><strong>And then I make some changes and persist the changes to the database.</strong></p>
  
  <p>Once the changes have been saved, I then try and add another child object</p>
  
  <pre><code>myClass.AddToChildren(new SomeChildObject("Another One!!!"));
  </code></pre>
  
  <p>And it falls over with "InvalidOperationException: The Collection is ReadOnly"</p>
  
  <p>Seems the NHibernate is doing something somewhere in it's proxy. Does anyone know how to resolve this issue?</p>
  
  <p>Thanks in advance.</p>