title: Fluent NHibernate: foreign key not null problem
body: <p>I have the following domain classes:</p>
  
  <pre><code>public class Installation : Entity&lt;Installation&gt;
  {        
      public virtual string Name { get; set; }
      public virtual IList&lt;Institution&gt; Institutions { get; set; }
  
      public Installation()
      {
          Institutions = new List&lt;Institution&gt;();
      }
  }
  public class Institution : Entity
  {
      public virtual string Name { get; set; }
      public virtual string Address { get; set; }
      public virtual string City { get; set; }
      public virtual Installation Installation { get; set; }        
  }
  </code></pre>
  
  <p>I have made the Entity base class according to the following <a href="http://dotnetslackers.com/articles/ado%5Fnet/Your-very-first-NHibernate-application-Part-2.aspx" rel="nofollow noreferrer">post</a>. I have the following mappings defined:</p>
  
  <pre><code>public class InstitutionMapping : ClassMap&lt;Institution&gt; 
  {
      public InstitutionMapping()
      {
          WithTable("Institution");
          Id(i =&gt; i.Id).GeneratedBy.Guid();
          Map(i =&gt; i.Name).Not.Nullable().WithLengthOf(50);
          Map(i =&gt; i.Address).Not.Nullable().WithLengthOf(50);
          Map(i =&gt; i.City).Not.Nullable().WithLengthOf(50);
          References(i =&gt; i.Installation).ColumnName("InstallationId").Not.Nullable().WithForeignKey();
      }
  }
  public class InstallationMapping : ClassMap&lt;Installation&gt;
  {
      public InstallationMapping()
      {
          WithTable("Installation");
          Id(i =&gt; i.Id).GeneratedBy.Guid();
          Map(i =&gt; i.Name).Not.Nullable().WithLengthOf(50);
          HasMany&lt;Institution&gt;(i =&gt; i.Institutions).KeyColumnNames.Add("InstallationId").Cascade.All();
      }
  }
  </code></pre>
  
  <p>When I run the following code:</p>
  
  <pre><code>Installation installation = TestHelper.CreateAnonymousInstallation();
  installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
  installation.Institutions.Add(TestHelper.CreateAnonymousInstitution());
  session.Save(installation);
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>NHibernate.PropertyValueException:
    not-null property references a null or
    transient value.</p>
  </blockquote>
  
  <p>How to overcome the problem?</p>
  
  <p>Thanks in advance
  Lukasz Glaz</p>