title: Map to view on custom field name - one-to-one relationship
body: <p>I am unable to get NHibernate to map the <code>AccountCode</code> column of the <code>Beneficiary</code> table <code>AccountCode</code> column in this one-to-one relationship (each <code>Account</code> has a single <code>Beneficiary</code>, each <code>Beneficiary</code> has a single <code>Account</code>).</p>
  
  <p>Classes:</p>
  
  <pre><code>public class Account
  {
      ...
      public virtual string Name { get; protected set; }
      public virtual string Code { get; protected set; }
  }
  
  public class Beneficiary
  {
      ...
      public virtual int Id { get; set; }
      public virtual string Name { get; protected set; }
      public virtual Account Account { get; protected set; }
      public virtual BeneficiaryGroup Group { get; protected set; }
  }
  </code></pre>
  
  <p>SQL:</p>
  
  <pre><code>CREATE VIEW dbo.Account AS
      SELECT DISTINCT RTRIM(LTRIM(ACCNT_NAME)) AS Name, 
                      RTRIM(LTRIM(ACCNT_CODE)) AS Code
      FROM myremoteserver.schema.tablename
      WHERE ACCNT_TYPE NOT IN ('B', 'P')
  
  
  CREATE TABLE dbo.Beneficiary
  (
      Id INT IDENTITY(1,1) NOT NULL, 
      BeneficiaryGroupId INT NOT NULL CONSTRAINT FK_Beneficiaries_BeneficiaryGroup FOREIGN KEY REFERENCES dbo.BeneficiaryGroup (Id), 
      Name VARCHAR(100) NOT NULL,
      AccountCode VARCHAR(100) NOT NULL,
      CONSTRAINT PK_Beneficiary PRIMARY KEY (Id)
  )
  </code></pre>
  
  <p>When trying to use <code>HasMany</code> and different variants, NHibernate tries to join on the <code>Beneficiary.Id</code> column. </p>
  
  <p>I had tried different variations of <code>Map</code>, <code>References</code>, <code>Join</code> (which tells me that the join already exists) and <code>HasMany</code> (which fails, as the relationship is indeed one-to-one).</p>
  
  <p>How can I get NHibernate to map these two classes correctly to their columns?</p>
  
  <hr>
  
  <p>When trying the different fluent mappings, in my <code>IAutoMappingOverride&lt;Beneficiary&gt;</code>, the following happens:</p>
  
  <pre><code>mapping.HasOne(b =&gt; b.Account);
  mapping.HasOne(b =&gt; b.Account).PropertyRef(sa =&gt; sa.Code);
  mapping.HasOne(b =&gt; b.Account).PropertyRef(sa =&gt; sa.Code).ForeignKey("none");
  </code></pre>
  
  <p>The generated SQL uses the <code>Beneficiary.Id</code> field instead of the <code>Beneficiary.AccountCode</code>. (before you ask, I am using "none" since <code>Account</code> is a view, it can't have a key).</p>
  
  <pre><code>mapping.Join("AccountCode", x =&gt; x.References(y =&gt; y.Account));
  mapping.Join("Account", b =&gt; b.KeyColumn("AccountCode"));
  </code></pre>
  
  <p>Result in <code>Tried to add join to table 'Account' when already added.</code>.</p>
  
  <p>And:</p>
  
  <pre><code>mapping.Map(b =&gt; b.Account, "AccountCode");
  mapping.Map(b =&gt; b.Account).ReadOnly().Column("AccountCode");
  </code></pre>
  
  <p>Result in: </p>
  
  <blockquote>
    <p>Could not determine type for: .Account, , Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: 
    NHibernate.Mapping.Column(AccountCode)</p>
  </blockquote>
  
  <pre><code>mapping.References(b =&gt; b.Account).Column("Code");
  </code></pre>
  
  <p>Results in <code>Invalid column name 'Code'.</code>.</p>
  
  <p>And:</p>
  
  <pre><code>mapping.References(b =&gt; b.Account).Column("AccountCode");
  mapping.References(b =&gt; b.Account).Column("AccountCode").Access.Property();
  </code></pre>
  
  <p>Overrides all my <code>IReferenceConvention</code> overrides (mapping some classes to have a <code>&lt;class name&gt;Code</code> key column).</p>
  
  <p>When trying <code>HasMany</code>:</p>
  
  <pre><code>mapping.HasMany&lt;Account&gt;(b =&gt; b.Account).KeyColumn("AccountCode");
  </code></pre>
  
  <blockquote>
    <p>Custom type does not implement UserCollectionType: .Account</p>
  </blockquote>