title: Fluent nhibernate one to one mapping
body: <p>How do I make a one to one mapping.</p>
  <pre class="lang-cs prettyprint-override"><code>public class Setting
  {
      public virtual Guid StudentId { get; set; }
      public virtual DateFilters TaskFilterOption { get; set; }
      public virtual string TimeZoneId { get; set; }
      public virtual string TimeZoneName { get; set; }
      public virtual DateTime EndOfTerm { get; set; }
      public virtual Student Student { get; set; }
  }
  </code></pre>
  <p>Setting Class map:</p>
  <pre class="lang-cs prettyprint-override"><code>public SettingMap() 
  {
      // Id(Reveal.Member&lt;Setting&gt;(&quot;StudentId&quot;)).GeneratedBy.Foreign(&quot;StudentId&quot;);
       
      //Id(x =&gt; x.StudentId);
         
      Map(x =&gt; x.TaskFilterOption)
          .Default(DateFilters.All.ToString())
          .NvarcharWithMaxSize()
          .Not.Nullable();
          
      Map(x =&gt; x.TimeZoneId)
          .NvarcharWithMaxSize()
          .Not.Nullable();
          
      Map(x =&gt; x.TimeZoneName)
          .NvarcharWithMaxSize()
          .Not.Nullable();
          
      Map(x =&gt; x.EndOfTerm)
          .Default(&quot;5/21/2011&quot;)
          .Not.Nullable();
         
      HasOne(x =&gt; x.Student);
  }
  </code></pre>
  <hr />
  <p>Student Class map</p>
  <pre class="lang-cs prettyprint-override"><code>public class StudentMap: ClassMap&lt;Student&gt; 
  {
      public StudentMap() 
      {
          Id(x =&gt; x.StudentId);
          
          HasOne(x =&gt; x.Setting)
              .Cascade.All();
      }
  }
  
  public class Student
  {
      public virtual Guid StudentId { get; private set; }
      public virtual Setting Setting { get; set; }
  }
  </code></pre>
  <hr />
  <p>Now every time I try to create a settings object and save it to the database it crashes.</p>
  <pre class="lang-cs prettyprint-override"><code>Setting setting = new Setting 
  {
      TimeZoneId = viewModel.SelectedTimeZone,
      TimeZoneName = info.DisplayName,
      EndOfTerm = DateTime.UtcNow.AddDays(-1),
      Student = student
  };
  </code></pre>
  <blockquote>
  <p>The INSERT statement conflicted with the FOREIGN KEY constraint &quot;FK_Settings_Students&quot;. The conflict occurred in database &quot;Database&quot;, table &quot;dbo.Students&quot;, column 'StudentId'.
  The statement has been terminated.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</p>
  </blockquote>
  <blockquote>
  <p>Exception Details: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint &quot;FK_Settings_Students&quot;. The conflict occurred in database &quot;Database&quot;, table &quot;dbo.Students&quot;, column 'StudentId'.
  The statement has been terminated.</p>
  </blockquote>
  <p>What am I missing?</p>
  <p><strong>Edit</strong></p>
  <pre class="lang-cs prettyprint-override"><code>public class StudentMap: ClassMap&lt;Student&gt; 
  {
      public StudentMap() 
      {
          Id(x =&gt; x.StudentId)
              .GeneratedBy.Guid();
          
          HasOne(x =&gt; x.Setting)
              .PropertyRef(&quot;Student&quot;)
              .Cascade.All();
      }
  }
  
  public class SettingMap: ClassMap&lt;Setting&gt; 
  {
      public SettingMap() 
      {
          Id(x =&gt; x.StudentId)
              .GeneratedBy.Guid();
              
          Map(x =&gt; x.TaskFilterOption)
              .Default(DateFilters.All.ToString())
              .NvarcharWithMaxSize().Not.Nullable();
          
          Map(x =&gt; x.TimeZoneId)
              .NvarcharWithMaxSize().Not.Nullable();
          Map(x =&gt; x.TimeZoneName)
              .NvarcharWithMaxSize().Not.Nullable();
              
          Map(x =&gt; x.EndOfTerm)
              .Default(&quot;5/21/2011&quot;).Not.Nullable();
          References(x =&gt; x.Student).Unique();
      }
  }
  </code></pre>
  <hr />
  <pre class="lang-cs prettyprint-override"><code>Setting setting = new Setting 
  {
      TimeZoneId = viewModel.SelectedTimeZone,
      TimeZoneName = info.DisplayName,
      EndOfTerm = DateTime.UtcNow.AddDays(-1),
      Student = student
  };
  
  studentRepo.SaveSettings(setting);
  studentRepo.Commit();
  </code></pre>
  <p>I get these error for both ways</p>
  <blockquote>
  <p>Invalid index 5 for this SqlParameterCollection with Count=5. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</p>
  </blockquote>
  <blockquote>
  <p>Exception Details: System.IndexOutOfRangeException: Invalid index 5 for this SqlParameterCollection with Count=5. Source Error: Line 76:             using (ITransaction transaction = session.BeginTransaction()) Line 77:   { Line 78:                 transaction.Commit(); Line 79:         } Line 80:         }</p>
  </blockquote>