title: Fluent-Nhibernate HasMany property reference on an object
body: <p>A referral has many leads. The entities are however, related by an agent identifier. Within my referral entity I ended up having to add an integer property mapped to the agent_id column in order to make the mappings work correctly. </p>
  
  <p>If I remove the AgentID property from the entity and perform the mapping on the "Agent" object like so:</p>
  
  <pre><code>    HasMany(x =&gt; x.Leads)
          .AsBag()
          .KeyColumn("Agent_Id")
          .PropertyRef("Agent");
  </code></pre>
  
  <p>I run into an error: </p>
  
  <blockquote>
    <p>Object does not match target type.</p>
    
    <p>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>
    
    <p>Exception Details: System.Reflection.TargetException: Object does not
    match target type.</p>
  </blockquote>
  
  <p>I guess, I'm asking if this is an acceptable solution? The additional AgentID property won't be used anywhere other than within the property reference. Is there another way to perform this mapping within having to change the domain model as it cannot be changed at this time.</p>
  
  <p><em>The working mappings</em>:</p>
  
  <pre><code>public class Referral
  {
      public virtual int Id { get; set; }
      public virtual int AgentID { get; set; }
      public virtual Agent Agent { get; set; }
      public virtual int? PositionNumber { get; set; }
      public virtual DateTime? LastReferralDate { get; set; }
      public virtual Account Account { get; set; }
  
      public virtual IEnumerable&lt;Lead&gt; Leads { get; set; }
  }
  
  public ReferralMap()
  {
      Table("Referral");
      LazyLoad();
      Id(x =&gt; x.Id).GeneratedBy.Identity().Column("Id");
      Map(x =&gt; x.PositionNumber).Column("PositionNumber");
      Map(x =&gt; x.LastReferralDate).Column("LastReferralDate");
      Map(x =&gt; x.AgentID).Column("Agent_ID");
      References(x =&gt; x.Agent).Column("Agent_ID");
      References(x =&gt; x.Account).Column("Account_id");
  
      HasMany(x =&gt; x.Leads)
          .AsBag()
          .KeyColumn("Agent_Id")
          .PropertyRef("AgentID");
  
  }
  </code></pre>