title: Fluent NHibernate - How to map a non nullable foreign key that exists in two joined tables
body: <p>I'm mapping a set of membership classes for my application using Fluent NHibernate.  I'm mapping the classes to the asp.net membership database structure.  The database schema relevant to the problem looks like this:</p>
  
  <pre><code>ASPNET_USERS
  UserId        PK
  ApplicationId FK NOT NULL
  other user columns ...
  
  ASPNET_MEMBERSHIP
  UserId        PK,FK
  ApplicationID FK NOT NULL
  other membership columns...
  </code></pre>
  
  <p>There is a one to one relationship between these two tables.  I'm attempting to join the two tables together and map data from both tables to a single 'User' entity which looks like this:</p>
  
  <pre><code>public class User
  {
      public virtual Guid Id { get; set; }
      public virtual Guid ApplicationId { get; set; }
  
      // other properties to be mapped from aspnetuser/membership tables ...
  </code></pre>
  
  <p>My mapping file is as follows:</p>
  
  <pre><code>public class UserMap : ClassMap&lt;User&gt;
  {
      public UserMap()
      {
          Table("aspnet_Users");
          Id(user =&gt; user.Id).Column("UserId").GeneratedBy.GuidComb();
          Map(user =&gt; user.ApplicationId);
          // other user mappings
  
          Join("aspnet_Membership", join =&gt; {
              join.KeyColumn("UserId");
              join.Map(user =&gt; user.ApplicationId);
              // Map other things from membership to 'User' class
          }
      }
  }
  </code></pre>
  
  <p>If I try to run with the code above I get a FluentConfiguration exception</p>
  
  <blockquote>
    <p>Tried to add property 'ApplicationId' when already added.</p>
  </blockquote>
  
  <p>If I remove the line <em>"Map(user => user.ApplicationId);"</em> or change it to <em>"Map(user => user.ApplicationId)<strong>.Not.Update().Not.Insert();</strong>"</em> then the application runs but I get the following exception when trying to insert a new user:</p>
  
  <blockquote>
    <p>Cannot insert the value NULL into column 'ApplicationId', table 'ASPNETUsers_Dev.dbo.aspnet_Users'; column does not allow nulls. INSERT fails.
    The statement has been terminated.</p>
  </blockquote>
  
  <p>And if I leave the <em>.Map(user => user.ApplicationId)</em> as it originally was and  make either of those changes to the <strong><em>join</strong>.Map(user => user.ApplicationId)</em> then I get the same exception above except of course the exception is related to an insert into the aspnet_Membership table</p>
  
  <p>So... how do I do this kind of mapping assuming I can't change my database schema?</p>