title: fluent nhibernate mapping issue: many-to-one...could not insert: [Entity]
body: <p>I have a one-to-many relationship between Foo and Bar and I cannot perform an insert (I've only tried select and insert so far...select seems to be working just fine, insert fails every time).</p>
  
  <p><strong>Table Definitions:</strong></p>
  
  <pre><code>CREATE TABLE [Bar](
  [Id] [int] IDENTITY(1,1) NOT NULL,
  [DateField] [datetime] NULL,
  [StringField] [varchar](8000) NULL
  CONSTRAINT [PK_Bar] PRIMARY KEY CLUSTERED([Id] ASC)) ON PRIMARY
  
  CREATE TABLE [Foo](
  [Id] [int] IDENTITY(1,1) NOT NULL,
  [BarId] [int] NOT NULL,
  [DateField] [date] NOT NULL,
  [StringField] [varchar](100) NOT NULL,
  [GorpId] [int] NOT NULL,
  [BoolField] [bit] NOT NULL
  CONSTRAINT [PK_Foo] PRIMARY KEY CLUSTERED([Id] ASC))
  
  ALTER TABLE [dbo].[Foo]  WITH CHECK ADD  CONSTRAINT [FK_Foo_Bar] FOREIGN KEY([BarId])
  REFERENCES [dbo].[Bar] ([Id])
  GO
  </code></pre>
  
  <p><strong>Class Definitions:</strong></p>
  
  <pre><code>public class Foo
  {
      public virtual int Id { get; set; }
      public virtual Bar Bar{ get; set; }
      public virtual DateTime DateField{ get; set; }
      public virtual string StringField{ get; set; }
      public virtual Gorp Gorp{ get; set; }
      public virtual bool BoolField{ get; set; }
  }
  
  public class Bar
  {
      public virtual int Id { get; set; }
      public virtual DateTime DateField{ get; set; }
      public virtual string StringField{ get; set; }
  
      public virtual ICollection&lt;Foo&gt; Foos{ get; set; }
  
      public Bar()
      {
          Foos= new List&lt;Foo&gt;();
      }
  
      public virtual void AddFoo(Foo foo)
      {
          foo.Bar = this;
          Foos.Add(foo);
      }
  }
  </code></pre>
  
  <p><strong>Mappings:</strong></p>
  
  <pre><code>public class FooMap : ClassMap&lt;Foo&gt;
  {
      public FooMap()
      {
          Id(x =&gt; x.Id).UnsavedValue(0).GeneratedBy.Identity();
          Map(x =&gt; x.DateField);
          Map(x =&gt; x.BoolField);
          Map(x =&gt; x.StringField);
          References(x =&gt; x.Gorp)
            .Column("GorpId")
            .Class&lt;Gorp&gt;();
          References(x =&gt; x.Bar)
            .Column("BarId")
            .Not.Nullable();
      }
  }
  
  public class BarMap : ClassMap&lt;Bar&gt;
  {
      Id(x =&gt; x.Id).UnsavedValue(0).GeneratedBy.Identity();
      Map(x =&gt; x.DateField);
      Map(x =&gt; x.StringField);
  
      HasMany&lt;Foo&gt;(x =&gt; x.Foos)
        .AsBag()
        .Cascade.SaveUpdate()
        .ForeignKeyConstraintName("FK_Foo_Bar")
        .Inverse()
        .KeyColumn("BarId")
        .Not.KeyNullable();
  }
  </code></pre>
  
  <p><strong>Generated XML:</strong></p>
  
  <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true"&gt;
      &lt;class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="MyApp.Domain.Model.Bar, MyApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" table="`Bar`"&gt;
          &lt;id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0"&gt;
              &lt;column name="Id" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;property name="DateField" type="System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
              &lt;column name="DateField" /&gt;
          &lt;/property&gt;
          &lt;property name="StringField" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
              &lt;column name="StringField" /&gt;
          &lt;/property&gt;
          &lt;bag cascade="save-update" inverse="true" name="Foos" mutable="true"&gt;
              &lt;key foreign-key="FK_Foo_Bar" not-null="true"&gt;
                  &lt;column name="BarId" /&gt;
              &lt;/key&gt;
              &lt;one-to-many class="MyApp.Domain.Model.Foo, MyApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" /&gt;
          &lt;/bag&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true"&gt;
      &lt;class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="MyApp.Domain.Model.Foo, MyApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" table="`Foo`"&gt;
          &lt;id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0"&gt;
              &lt;column name="Id" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;property name="DateField" type="System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
              &lt;column name="DateField" /&gt;
          &lt;/property&gt;
          &lt;property name="BoolField" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
              &lt;column name="BoolField" /&gt;
          &lt;/property&gt;
          &lt;property name="StringField" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
               &lt;column name="StringField" /&gt;
          &lt;/property&gt;
          &lt;many-to-one class="MyApp.Domain.Model.Gorp, MyApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" name="Gorp"&gt;
              &lt;column name="GorpId" /&gt;
          &lt;/many-to-one&gt;
          &lt;many-to-one class="MyApp.Domain.Model.Bar, MyApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" insert="false" name="Bar" update="false"&gt;
              &lt;column name="BarId" not-null="true" /&gt;
          &lt;/many-to-one&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>Exception:</strong></p>
  
  <p>could not insert: [MyApp.Domain.Model.Foo][SQL: INSERT INTO [Foo] (DateField, BoolField, StringField, GorpId, BarId) VALUES (?, ?, ?, ?, ?); select SCOPE_IDENTITY()]</p>
  
  <p>What am I missing/misunderstanding?</p>