title: NHibernate - Wrong Columns on Queries
body: <p>I'm getting an intermittant problem with NHibernate where it generates a query for an entity, but replaces one of the columns with a column from completely different (and unrelated) entity. </p>
  
  <p>It only ever replaces a single column, and is generally solved by restarting the application (though sometimes it takes a couple of attempts).</p>
  
  <ul>
  <li>ASP.NET application (.NET 4.0)</li>
  <li>SessionFactory created during Application_Start</li>
  <li>NHibernate 3.3.1- All mappings/configuration done via Mapping By Code</li>
  <li>Using Nhibernate Criteria</li>
  </ul>
  
  <p>Any input on this would be much appreciated!</p>
  
  <p><strong>Entity</strong></p>
  
  <pre><code>public class LiquiditySourceItem : RunDataEntity, IEntity&lt;int&gt;
      {
          public virtual int Id { get; protected internal set; }
          public virtual int IdentID { get; protected internal set; }
          public virtual string Portfolio { get; protected internal set; }
          public virtual string ProfitCentre { get; protected internal set; }
          public virtual DateTime? MaturityDate { get; protected internal set; }
          public virtual string Curr1 { get; protected internal set; }
          public virtual string Curr2 { get; protected internal set; }
          public virtual decimal Reval { get; protected internal set; }
          public virtual string ContractType { get; protected internal set; }
          public virtual string ContractType2 { get; protected internal set; }
          public virtual string ContractCode { get; protected internal set; }
          public virtual decimal AmountSignedTradeUnit { get; protected internal set; }
          public virtual decimal Amount2Signed { get; protected internal set; }
          public virtual decimal SpotDelta { get; protected internal set; }
          public virtual string TradeRevalCurr { get; protected internal set; }
      }
  </code></pre>
  
  <p><strong>Entity Mapping</strong></p>
  
  <pre><code>public LiquiditySourceItemMap()
  {
      Id(x =&gt; x.Id, map =&gt; map.Column("RowId"));
      Property(x =&gt; x.IdentID, map =&gt; map.Column("IdentID"));
      Property(x =&gt; x.Portfolio, map =&gt; map.Column("Portfolio"));
      Property(x =&gt; x.ProfitCentre, map =&gt; map.Column("ProfitCentre"));
      Property(x =&gt; x.MaturityDate, map =&gt; map.Column("Con_Expiry"));
      Property(x =&gt; x.BuySell, map =&gt; map.Column("BS"));
      Property(x =&gt; x.Curr1, map =&gt; map.Column("Curr1"));
      Property(x =&gt; x.Curr2, map =&gt; map.Column("Curr2"));
      Property(x =&gt; x.Reval, map =&gt; map.Column("Reval"));
      Property(x =&gt; x.ContractType, map =&gt; map.Column("ContractType"));
      Property(x =&gt; x.ContractType2, map =&gt; map.Column("ContractType2"));
      Property(x =&gt; x.ContractCode, map =&gt; map.Column("ContractCode"));
      Property(x =&gt; x.AmountSignedTradeUnit, map =&gt; map.Column("AmountSignedTradeUnit"));
      Property(x =&gt; x.Amount2Signed, map =&gt; map.Column("Amount2Signed"));
      Property(x =&gt; x.ValSpot, map =&gt; map.Column("Val_Spot"));
      Property(x =&gt; x.SpotDelta, map =&gt; map.Column("SpotDelta"));
      Property(x =&gt; x.TradeRevalCurr, map =&gt; map.Column("Traderevalcurr"));
      Property(x =&gt; x.SourceReport, map =&gt; map.Column("SourceReport"));
      ManyToOne(x =&gt; x.RunContext, map =&gt; map.Column("RunContextID"));
      Table("Staging.vw_Liquidity");
  }
  </code></pre>
  
  <p><strong>Report Entity</strong></p>
  
  <pre><code>public class BusinessBreakdownStandardPosition : ReportRunDataEntity, IEntity&lt;long&gt;
      {
          public virtual long Id { get; set; }
          public virtual decimal FinalNettingAmountUSD { get; set; }
          public virtual decimal InitialChargeAmountUSD { get; set; }
          public virtual BusinessBreakdownInitialPrr InitialPrr { get; set; }
          public virtual IEnumerable&lt;FinalInstrumentPosition&gt; FinalInstrumentPositions { get; set; }
          public virtual decimal CreditEventPaymentUSD { get; set; }
          public virtual decimal ValuationChangeIncreaseUSD { get; set; }
          public virtual decimal ValuationChangeDecreaseUSD { get; set; }
          public virtual string ReportKey { get; set; }
          public virtual decimal USDCharge { get; set; }
          public virtual decimal USDChargeICG { get; set; }
          public virtual string InstrumentType { get; set; } 
  }
  </code></pre>
  
  <p><strong>Report Entity Mapping</strong></p>
  
  <pre><code>public class BusinessBreakdownStandardPositionMap : ClassMapping&lt;BusinessBreakdownStandardPosition&gt;
      {
          public BusinessBreakdownStandardPositionMap()
          {
              Id(x =&gt; x.Id,
                 m =&gt;
                     {
                         m.Column("BusinessBreakdownStandardPositionID");
                         m.Generator(Generators.HighLow,
                                     g =&gt;
                                     g.Params(
                                         new
                                             {
                                                 table = "dbo.HiValue",
                                                 max_lo = 10000,
                                                 Where = string.Format("EntityName = 'BusinessBreakdownStandardPosition'")
                                             }));
                     });
              Property(x =&gt; x.FinalNettingAmountUSD, map =&gt; map.Column("FinalNettingAmountUSD"));
              Property(x =&gt; x.InitialChargeAmountUSD, map =&gt; map.Column("InitialAmountUSD"));
              Property(x =&gt; x.CreditEventPaymentUSD);
              Property(x =&gt; x.ValuationChangeDecreaseUSD);
              Property(x =&gt; x.ValuationChangeIncreaseUSD);
              Property(x =&gt; x.USDCharge);
              Property(x =&gt; x.USDChargeICG);
              Property(x=&gt;x.InstrumentType);
              ManyToOne(p =&gt; p.RunContext, map =&gt; map.Column("ReportRunContextID"));
              ManyToOne(p =&gt; p.InitialPrr, m =&gt;
                  {
                      m.Column("InitialPrrID");
                      m.Cascade(Cascade.All);
                  });
              Property(x =&gt; x.ReportKey);
              Bag(x =&gt; x.FinalInstrumentPositions, collectionMapping =&gt;
                  {
                      collectionMapping.Table("Reporting.BusinessBreakdownFinalInstrumentPositionStandardPositionMap");
                      collectionMapping.Cascade(Cascade.All);
                      collectionMapping.Key(k =&gt; k.Column("StandardPositionID"));
                  }, mapping =&gt; mapping.ManyToMany(y =&gt; y.Column("FinalInstrumentPositionID")));
              Table("Reporting.BusinessBreakdownStandardPosition");
          }
      }
  </code></pre>
  
  <p><strong>SQL Query, Generated By NHibernate</strong></p>
  
  <pre><code>SELECT
      this_.RowId AS RowId47_0_,
      this_.IdentID AS IdentID47_0_,
      this_.Portfolio AS Portfolio47_0_,
      this_.ProfitCentre AS ProfitCe4_47_0_,
      this_.Con_Expiry AS Con5_47_0_,
      this_.BS AS BS47_0_,
      this_.Curr1 AS Curr7_47_0_,
      this_.Curr2 AS Curr8_47_0_,
      this_.Reval AS Reval47_0_,
      this_.ContractType AS Contrac10_47_0_,
      this_.ContractType2 AS Contrac11_47_0_,
      this_.ContractCode AS Contrac12_47_0_,
      this_.AmountSignedTradeUnit AS AmountS13_47_0_,
      this_.Amount2Signed AS Amount14_47_0_,
      this_.Val_Spot AS Val15_47_0_,
      this_.SpotDelta AS SpotDelta47_0_,
      this_.InitialAmountUSD AS Initial17_47_0_,
      this_.RunContextID AS RunCont18_47_0_,
      this_.SourceReport AS Sou19_47_0_
  FROM Staging.vw_Liquidity this_
  </code></pre>
  
  <p><strong>Exception</strong></p>
  
  <pre><code>System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'InitialAmountUSD'.
  </code></pre>
  
  <p>As you can see, nhibernate has replaced the LiquiditySourceItem column 'Traderevalcurr' with 'InitialAmountUSD', which belongs to the BusinessBreakdownStandardPosition entity. These entities have no relationship whatsoever. Otherwise, the SQL is exactly as you'd expect( including column order).</p>
  
  <p><strong>Observations</strong></p>
  
  <ul>
  <li>The wrong column is always a valid column in a different mapped entity</li>
  <li>The wrong column will replace an existing one</li>
  <li>The issue sometimes ocurrs between other entities. Again, there's no relationship between these</li>
  </ul>
  
  <p>Any thoughts?</p>