title: Fluent Nhibernate - Mapping a list results in NullReferenceException?
body: <p>I have the following classes and fluent mappings:</p>
  
  <pre><code>  public class A {
      public virtual int Id { get; private set; }
      public virtual string MyString { get; set; }
      public virtual IList&lt;B&gt; MyChildren { get; set; }
   }
  
    public class B {
      public virtual int Id { get; private set; }
      public virtual DateTime TheDate { get; set; }
    }
  
    public sealed class AMap : ClassMap&lt;A&gt; {
      public AMap() {
        Id(x =&gt; x.Id).GeneratedBy.Native().UnsavedValue(0);
        Map(x =&gt; x.MyString);
        HasMany(x =&gt; x.MyChildren).AsList(x =&gt; x.Column("Ordinal")).KeyColumn("AId").Not.KeyNullable();
      }
    }
  
    public sealed class BMap : ClassMap&lt;B&gt; {
      public BMap() {
        Id(x =&gt; x.Id).GeneratedBy.Native().UnsavedValue(0);
        Map(x =&gt; x.TheDate);
      }
    }
  </code></pre>
  
  <p>This results in the following mapping for A:</p>
  
  <pre><code>  &lt;class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="A" table="`A`"&gt;
      &lt;id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0"&gt;
        &lt;column name="Id" /&gt;
        &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="MyString" type="AnsiString"&gt;
        &lt;column name="MyString" length="150" not-null="true" /&gt;
      &lt;/property&gt;
      &lt;list name="MyChildren" mutable="true"&gt;
        &lt;key not-null="true"&gt;
          &lt;column name="AId" /&gt;
        &lt;/key&gt;
        &lt;index&gt;
          &lt;column name="Ordinal" /&gt;
        &lt;/index&gt;
        &lt;one-to-many class="B" /&gt;
      &lt;/list&gt;
    &lt;/class&gt;
  </code></pre>
  
  <p>But when I actually try to save an instance of A, I get a <code>NullReferenceException</code>:</p>
  
  <pre><code>System.NullReferenceException : Object reference not set to an instance of an object.
  at NHibernate.Collection.PersistentList.GetSnapshot(ICollectionPersister persister)
  at NHibernate.Engine.CollectionEntry..ctor(ICollectionPersister persister, IPersistentCollection collection)
  at NHibernate.Engine.StatefulPersistenceContext.AddNewCollection(ICollectionPersister persister, IPersistentCollection collection)
  at NHibernate.Event.Default.WrapVisitor.ProcessArrayOrNewCollection(Object collection, CollectionType collectionType)
  at NHibernate.Event.Default.WrapVisitor.ProcessCollection(Object collection, CollectionType collectionType)
  at NHibernate.Event.Default.AbstractVisitor.ProcessValue(Object value, IType type)
  at NHibernate.Event.Default.WrapVisitor.ProcessValue(Int32 i, Object[] values, IType[] types)
  at NHibernate.Event.Default.AbstractVisitor.ProcessEntityPropertyValues(Object[] values, IType[] types)
  at NHibernate.Event.Default.AbstractSaveEventListener.VisitCollectionsBeforeSave(Object entity, Object id, Object[] values, IType[] types, IEventSource source)
  at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
  at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
  at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
  at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
  at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
  at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
  at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
  at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
  at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
  at NHibernate.Impl.SessionImpl.Save(Object obj)
  </code></pre>
  
  <p>What am I doing wrong here?</p>
  
  <p>Clarification:  This is how I'm using the classes:</p>
  
  <pre><code>var a = new A { MyChildren = new List&lt;B&gt; { new B { TheDate = DateTime.Now } } };
  a.MyChildren[0].Parent = a;
  
  session.Save(a);
  </code></pre>