title: NHibernate How to map foreign key relationship to multiple properties
body: <p>I'm using NHibernate as my ORM for a personal project of mine. I have a Postgre SQL database for storage and I'm trying to map a Many-To-One relationship between the tables.</p>
  
  <p>I have a web_exception table that look like this</p>
  
  <pre><code>CREATE TABLE logging.web_exception
  (
      web_exception_id integer NOT NULL DEFAULT nextval('logging.web_exception_web_exception_id_seq'::regclass),
      query_string character varying(2048) COLLATE pg_catalog."default",
      form text COLLATE pg_catalog."default",
      cookies text COLLATE pg_catalog."default",
      session text COLLATE pg_catalog."default",
      headers text COLLATE pg_catalog."default",
      exception text COLLATE pg_catalog."default" NOT NULL,
      url character varying(2048) COLLATE pg_catalog."default" NOT NULL,
      application_id integer,
      CONSTRAINT web_exception_pkey PRIMARY KEY (web_exception_id),
      CONSTRAINT fk_web_exception_application FOREIGN KEY (application_id)
          REFERENCES public.application (application_id) MATCH SIMPLE
          ON UPDATE NO ACTION
          ON DELETE NO ACTION
  )
  </code></pre>
  
  <p>I'm trying to map it to my WebException class that looks like this.</p>
  
  <pre><code>public class WebException
  {
      public virtual int WebExceptionId { get; set; }
      public virtual string QueryString { get; set; }
      public virtual string Form { get; set; }
      public virtual string Cookies { get; set; }
      public virtual string Session { get; set; }
      public virtual string Headers { get; set; }
      public virtual string Exception { get; set; }
      public virtual string Url { get; set; }
      public virtual int? ApplicationId { get; set; }
  
      public virtual Application Application { get; set; }
  }
  </code></pre>
  
  <p>I have run into an issue mapping the class with both the <code>ApplicationId</code> and the <code>Application</code> property. Both properties represent the foreign key to the <code>application</code> table I have defined in the database. One is obviously just the integer primary key value and the other is the entire entity representing a row in that table. In my mappings I have</p>
  
  <pre><code>Property(
      we =&gt; we.ApplicationId,
      m =&gt; m.Column(c =&gt; c.Name("application_id"))
  );
  ManyToOne(we =&gt; we.Application, mto =&gt; mto.Column("application_id"));
  </code></pre>
  
  <p>It looks like this causes NHibernate to think that properties should be mapped to 2 separate columns. I get this exception message, which is generated from NHibernates internals, when trying to save it to the database.</p>
  
  <blockquote>
    <p>Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index</p>
  </blockquote>
  
  <p>When I remove the <code>ManyToOne</code> mapping saving the record works just fine. However I would like the flexibility of having both properties. i.e. being able to simply set the <code>ApplicationId</code> property on save to create a relationship between the tables and being able to bring back the entire <code>Application</code> class as a member of the <code>WebException</code> class when needed. Is there any way I can achieve this?</p>