title: Fluent NHibernate One to Many Could Not Insert (foreign key constraint)
body: <p>I have an error when testing my <strong>Fluent NHibernate</strong> mappings with <strong>NUnit</strong>. The problem arise when Fluent NHibernate tries to persist one object (<strong>Contact</strong>) in the database before persisting it's child (<strong>ContactType</strong>), thus giving me a foreign key violation:</p>
  
  <pre><code>NHibernate.Exceptions.GenericADOException : could not insert: Contact [...]
  ----&gt; System.Data.SqlClient.SqlException : The INSERT statement conflicted with 
  the FOREIGN KEY constraint [...] with table ContactType
  </code></pre>
  
  <p><strong>EDIT: Modified mapping of supplier to use Inverse() on the HasMany(). Also providing example of my mapping test.</strong></p>
  
  <p>Now, the details.</p>
  
  <p>I have a <strong>Supplier</strong> that has many <strong>Contact</strong> (one-to-many). The <strong>Contact</strong> has many-to-one relation with an other table, <strong>ContactType</strong>. Keep in mind that I am simplifying the models here so we can focus on the problem.</p>
  
  <pre><code>public class SupplierMap : ClassMap&lt;Supplier&gt;
  {
          public SupplierMap() 
          {
              Table("Supplier");
              LazyLoad();
              Id(x =&gt; x.Id).GeneratedBy.Identity().Column("id");
              HasMany(x =&gt; x.Contacts).KeyColumn("supplierId").Cascade.All().Inverse();
          }
  }
  public class ContactMap : ClassMap&lt;Contact&gt; 
  {
      public ContactMap() 
      {
          Table("Contact");
          LazyLoad();
          Id(x =&gt; x.Id).GeneratedBy.Identity().Column("id");
          References(x =&gt; x.ContactType).Column("contactTypeId");
          References(x =&gt; x.Supplier).Column("supplierId").Cascade.All();
      }
  }
  public class ContactTypeMap : ClassMap&lt;ContactType&gt; 
  {
  
      public ContactTypeMap() {
          Table("ContactType");
          LazyLoad();
          Id(x =&gt; x.Id).GeneratedBy.Identity().Column("id");
      }
  }
  </code></pre>
  
  <p>Now when I test <strong>ContactMap</strong> the test runs just fine, no errors. When I run the SupplierMap test however, this is where I get the error. As expected, when I run the ContactMap test I get something like this in the trace:</p>
  
  <pre><code>INSERT INTO ContactType [...]
  INSERT INTO Contact [...]
  SELECT [...]
  [...]
  </code></pre>
  
  <p>ContactMap test: </p>
  
  <pre><code>[Test]
      public void CanCorrectlyMapContact()
      {
          new PersistenceSpecification&lt;Contact&gt;(Session)
      .CheckProperty(c =&gt; c.Id, 1)
      .CheckReference(c =&gt; c.ContactType, new ContactType() { Id = 1, Archived = false, DescriptionEn = Constants.LOREM_IPSUM_255, DescriptionFr = Constants.LOREM_IPSUM_255, NameEn = Constants.LOREM_IPSUM_50, NameFr = Constants.LOREM_IPSUM_50 })
      .CheckReference(c =&gt; c.Title, new Title() { Id = 1, Archived = false, NameEn = Constants.LOREM_IPSUM_255, NameFr = Constants.LOREM_IPSUM_255, SortOrder = 0})
      .CheckReference(c =&gt; c.Region, new Region() { Id = 1, Archived = false, NameEn = Constants.LOREM_IPSUM_128, NameFr = Constants.LOREM_IPSUM_128, SortOrder = 0 })
      .VerifyTheMappings();
      }
  </code></pre>
  
  <p>For <strong>SupplierMap</strong> test however, I tries to insert <strong>Contact</strong> first (without trying to insert <strong>ContactType</strong>). </p>
  
  <p>SupplierMap test:</p>
  
  <pre><code>[TestFixture]
  public class SupplierMapTest : DatabaseSetup
  {
      private IList&lt;Contact&gt; tmpList;
  
      public SupplierMapTest()
     {
         Contact tmpContact = new Contact()
         {
             Id = 1,
             FirstName = Constants.LOREM_IPSUM_128,
             LastName = Constants.LOREM_IPSUM_128,
             Address = Constants.LOREM_IPSUM_128,
             City = Constants.LOREM_IPSUM_128,
             Email = Constants.LOREM_IPSUM_128,
             FaxNumber = Constants.PHONE_NUMBER_50,
             PhoneNumber = Constants.PHONE_NUMBER_50,
             PostalCode = Constants.POSTAL_CODE_10,
             Archived = false
         };
         tmpContact.ContactType = new ContactType { Id = 1, Archived = false, DescriptionEn = Constants.LOREM_IPSUM_255, DescriptionFr = Constants.LOREM_IPSUM_255, NameEn = Constants.LOREM_IPSUM_50, NameFr = Constants.LOREM_IPSUM_50 };
         this.tmpList = new List&lt;Contact&gt;();
         this.tmpList.Add(tmpContact);
     }
  
      /// &lt;summary&gt;
      /// Verify if the maping is successfull
      /// &lt;/summary&gt;
      [Test]
      public void CanCorrectlyMapSupplier()
      {
  
          new PersistenceSpecification&lt;Supplier&gt;(Session)
          .CheckProperty(x =&gt; x.Id, 1)
          .CheckList(x =&gt; x.Contacts, this.tmpList)
          .VerifyTheMappings();
      }
  </code></pre>
  
  <p>This test results in the error at the top of this post.</p>
  
  <p>Thanks in advance for your help. I can also provide more details if needed.</p>